3

I've been having no end of trouble just getting started with C++. Seems like no matter what I do, I've got dozens of errors. I've taken to compiling example code from tutorials, but even the example code doesn't compile. For example, the code at: http://www.cppgameprogramming.com/cgi/nav.cgi?page=arrayclasses

gives me lots of flack when I try to:

$ g++ main.cpp -o dog

It says:

> main.cpp: In function 'int main()': main.cpp:18:15: warning: deleting
> array 'Dog myDogs [5]'
> C:\Users\bob-pc\Appdata\Local\Temp\ccTXXCOB.o:main.cpp:(.text+0x21):
> undefined rference to 'Dog::Dog()' C:\Users\bob-pc\Appdata\Local\Temp\ccTXXCOB.o:main.cpp:(.text+0x59):
> undefined rference to 'Dog::setAge(int)' C:\Users\bob-pc\Appdata\Local\Temp\ccTXXCOB.o:main.cpp:(.text+0x88):
> undefined rference to 'Dog::getAge()' collect2: Id returned 1 exit status

This isn't the first time I've tried straight copying example code & compiling it. I'm having lots of trouble even after I go over syntax, but I just can't seem to get headers & libraries working right. Any help much appreciated.

user229044
  • 232,980
  • 40
  • 330
  • 338
Jack
  • 2,229
  • 2
  • 23
  • 37
  • 6
    If you're just getting started with C++, please get [a proper introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). It'll cover issues like this and much more. – In silico Aug 08 '11 at 03:36

3 Answers3

4

You have to include dog.cpp in the compilation:

g++ main.cpp dog.cpp -o dog

Alternatively, you can compile dog.cpp by itself to an object file, and include that in the final compilation:

g++ dog.cpp -c -o dog.o

g++ main.cpp dog.o -o dog
user229044
  • 232,980
  • 40
  • 330
  • 338
  • Thank you! I still get an error of:main.cpp: In function 'int main()': main.cpp:18:15: warning: deleting array 'Dog myDogs [5]' Any thoughts? – Jack Aug 08 '11 at 03:39
  • @Jackalope: The code on that webpage is flat out wrong. You `delete[]` only when you use `new[]`. The declaration `Dog myDogs[5];` allocates an array of 5 `Dog`s on automatic storage. You don't need to call `delete[]` on arrays allocated on automatic storage; it'll be destroyed automatically at the end of the scope (in this case, the `main()` function). – In silico Aug 08 '11 at 03:40
  • 5
    @Jackalope This is terrible code, and you shouldn't be trying to learn *anything* from this website. Delete the line `delete [] myDogs;` Also warnings aren't "errors". Your program is successfully compiling, but the compiler is warning you that there could be trouble with some of your code. – user229044 Aug 08 '11 at 03:41
  • @meagar: True, but one should still strive to compile as cleanly as possible (no warnings, errors, etc). – In silico Aug 08 '11 at 03:43
  • 2
    @Jackalope: Like I said, please pick up [a good introductory C++ book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). I wouldn't use code from a website that demonstrates no understanding of array memory management. – In silico Aug 08 '11 at 03:44
  • @Jackalope: Unfortunately and coincidentally, game programmers *tend* to be some of the worse programmers I've ever seen, hands down. – GManNickG Aug 08 '11 at 06:36
  • Thanks, all of you, for your advice! I think I'll pick up a good book as you advise. – Jack Aug 08 '11 at 16:29
4

The example code is just plain wrong. :-)

//main.cpp
#include <iostream>
#include "Dog.h"

using namespace std;

int main()
{

    Dog myDogs[5];

    for (int i = 0; i <= 4; i++)
        myDogs[i].setAge( i * 2);

    for (int i = 0; i <= 4; i++)
        cout << "Dog number " << i << " is " << myDogs[i].getAge() << " years old!\n";

    delete [] myDogs;    <--- Don't do this!

    return 0;
}

You should not use delete if you have not used new to allocate the object. It this case the dogs will take care of themselves. Just remove the line with delete!

Bo Persson
  • 90,663
  • 31
  • 146
  • 203
0

It is really hard to determine what is going on without taking a look at your code. Make sure you're including the .o of the file where the class dog is defined.

Also if the array wasn't created with new[], don't use delete[].

Finally, I know you're not specifically asking about this, but it may be worth it to find a better resource online to learn C++. At least use one that doesn't have embarrassing mistakes.

carlosdc
  • 12,022
  • 4
  • 45
  • 62
  • More precisely, if the array wasn't allocated with `new[]`, don't use `delete[]`. In this case the array wasn't statically defined (it has automatic storage duration), but the `delete[]` is definitely wrong (it's likely to corrupt the program's internal memory management system). – Keith Thompson Aug 08 '11 at 04:43