Some of these errors will cause compilation errors. Some may compile but result in linking errors. Some will compile and link but cause errors while running. And some, you may get away with completely, but are just dumb things to do and will probably bite you another time.
const
const
when they
ought to be.const
in the class definition
but not in the member function implementation.cout << aDwarf.getName << endl;
cout << aDwarf.getName() << endl;
Students often have trouble getting their code to work, not because they have trouble with C++, but because they have trouble breaking a problem into pieces that make sense. This applies nearly as much to students who generally do well in the course as it does to those who struggle.
The general problem comes down to, "Can you say in a few words what the purpose of a section of code is?" The simpler your description, then the more likely your code actually does what you wanted.
for (int i = 0; i < MAX; ++i) {
// If we have found the target, do something with it
// and finish.
if (someTest(i)) {
doSomething(i);
break; // or perhaps return
}
}
try:
int target = 0;
// Find the target
while (target < MAX && !someTest(target))
++target;
// If we found it, do somthing with it.
if (target < MAX) doSomething(target);
Now your code is using the loop to find the target location. After
finding it, we can do whatever is needed. Is one piece of code clearly better
than the other? No. But getting the first approach to work correctly seems
to be harder to do consistently.someTest
and doSomething
above, makes the code easier to read and understand,
too. ifs >> name1 >> name2;
for( int j = 0; j<people.size(); j++ ) {
for( int k = 0; k<people.size(); k++ ) {
if(name1 == people[j]->getName()) {
if(name2 == people[k]->getName()) {
// if people with name1 and name2 exist in the vector of Person
// then make them to point to each other
people[j]->setSpouse( people[k] );
}
}
}
}
Maintained by John Sterling (jsterling@poly.edu). Last updated January 23, 2011