cc-june23

cc-june23 - CS 246 (Spring 2011) Comments and corrections...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 246 (Spring 2011) Comments and corrections for Thurs June 26 (Section 001) ======================================================== Example from lecture -------------------- Here is a short code example that I showed on the board during this lecture. I am posting it because it answers some questions that were also asked during Tuesday's lecture. Consider the two classes Posn and Person: class Posn { int x, y; // private public: Posn(int a, int b) { x = a; y = b; } }; class Person { Posn pos; // private public: Person(int a = 0, int b = 0) { pos = Posn(a,b); } }; int main() { Person p; } When we try to compile this code, we get "error: no matching function for call to 'Posn::Posn()'. This is because pos, an object member of Person, must be initialized at the point of declaration (i.e., when we say Posn pos;). This declaration assumes that we have a constructor for Posn with no parameters, and the error message we receive reminds us that such no such constructor exists. Here are four ways of dealing
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 12/08/2011 for the course CS 246 taught by Professor Wormer during the Spring '08 term at Waterloo.

Page1 / 2

cc-june23 - CS 246 (Spring 2011) Comments and corrections...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online