Classes - "myBotMain.o:myBotMain.cpp|| undefined reference to `bot::setName(std::string)'"

Ok, so I'm new to C++ and want to see where I'm messing this up.

I'm using code::blocks with Gnu GCC.


myBotMain.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22


#include "bot.hpp"
#include <iostream>
#include <string>

using namespace std;

int main() {

string nameInput;
string nameStore;
nameInput="test";
nameStore="test";
bot *mybot = new bot();
nameInput = "test";
mybot->setName(nameInput);
return 0;


}


bot.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23


#include "bot.hpp"
#include <string>

using namespace std;



string bot::setName(string wordThatIsName)
{
    MyName=wordThatIsName;
   
}

string bot::getName()
{

    return MyName;

}



bot.hpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

#ifndef bot_h
#define bot_h
#include <string>
using namespace std;
//put class members here

class bot {
string MyName;
public:
   void setName(string wordThatIsName);
   std::string  getName();
};

#endif


I keep receiving this error "myBotMain.o:myBotMain.cpp|| undefined reference to `bot::setName(std::string)'"

but setName looks fine to me :/

I want to use the default constructor/destructor
Last edited on
Your header file prototype for setName() doesn't match the definition in your source file (the return values differ).
That's not what it is. I removed the return MyName from setName function and I'm still getting this error.
Look at line 12 in bot.hpp. Then look at line 5. A bit redundant, don't you think?

Also, note how on line 11 you refer to setName as returning a void value, but on line 10 of bot.cpp it returns a string. Those are two different functions as far as the compiler is concerned.
<nolyc> Undefined reference is a linker error.
It's not a compile error. #includes don't help.
You did not define the thing in the error message, you forgot to link the file that defines it, you forgot to link to the library that defines it, or, if it's a static library, you have the wrong order on the linker command line.
Check which one. (Note that some linkers call it an unresolved external)


Also bot *mybot = new bot();
¿where did you "learn" that?
If you want to create an object use bot mybot;


Edit:
> I removed the return MyName from setName function and I'm still getting this error.
Look at the type
Last edited on
<quote>

<nolyc> Undefined reference is a linker error.
It's not a compile error. #includes don't help.
You did not define the thing in the error message, you forgot to link the file that defines it, you forgot to link to the library that defines it, or, if it's a static library, you have the wrong order on the linker command line.
Check which one. (Note that some linkers call it an unresolved external)
</quote>

Yeah, the problem was that my compiler couldn't find my header file. When I switched over to my Mac and in Xcode, I had no problems (once I corrected the other issues).

<quote>
Also bot *mybot = new bot();
¿where did you "learn" that?
If you want to create an object use bot mybot;
</quote>

This is correct if you want to allocate an object to the Heap.

bot mybot; stack allocates a bot object and is dealloc'd once it goes out of scope (in this case, leaves the main() function).

similiar to objective-C where you can:

objecttype *myobject = [[myobject alloc] init];

Actually, all objects are heap allocated in Objective-C.

Anyway, that's where I "learned" that.

Do yourself a favor and learn the difference between the heap and the stack.

<quote>
Edit:
> I removed the return MyName from setName function and I'm still getting this error.
Look at the type
</quote>

Yep, that was a problem.
Do yourself a favor and stop leaking resources.
Every `new' must have a corresponding `delete' where the destructor would be invoked and the memory released.

However, `Code that creates an object using new and then deletes it at the end of the same scope is ugly, error-prone, and inefficient.' (BJarne Stroustrup)


Don't simply write your code as you'll do in another language.
Topic archived. No new replies allowed.