Help with undefined reference when creating class object!

Hey, another confused beginner here looking for some help. Didn't sleep for a while so might just be brain freezing but need to finish my assignments tonight and can't seem to figure out the problem here.
The error message i get is "undefined reference to `Cat::Cat(std::string, std::string, int)" when I try to create my class object with the overload constructor. What am I missing?

Many thanks!
Heres the code;




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
Header file
#ifndef KATT4_H_INCLUDED
#define KATT4_H_INCLUDED
#include <string>

class Cat {
private:
std::string character;
std::string race;
int age;


public:
Cat();
Cat(std::string, std::string, int);

};

#endif // KATT4_H_INCLUDED

katt4.cpp file
#include "katt4.h"
#include <iostream>
#include <string>

Cat::Cat() {
}

Cat::Cat(std::string _character, std::string _race, int _age) {
character = _character;
race = _race;
age = _age;

include "katt4.h"


main.cpp

int main()
{
Cat RendemCat("Fat", "Zebracat", 24);

return 0;
}
When you linked, did you definitely link the object file that was made by compiling katt4.cpp ? (trick question; no, you did not)

Because there's nothing wrong with this code (except for a missing brace and so on that I expect is a cut and paste error). So basically, the object file that gets made when you compile katt4.cpp isn't being made, or it isn't being linked with the object file that gets made when you compile main.cpp

Your code is main.cpp is "looking" for the constructor code, and it can't find it.

If you're using an IDE, I expect that you haven't put all these files into your "project" or whatever your IDE calls it.

If you're building it yourself at the command line, the following build command works perfectly, so you must be missing katt4.cpp

g++ katt4.cpp main.cpp
Last edited on
Thank you for your response! I created the project with the main.cpp file and then added both files to it, so they are in the same project. :/ Im using Codeblocks 13.12!

thanks for your help!

I'm not familiar with your IDE, but this is a problem of telling your IDE what files you want it to compile and link, rather than a problem with your actual code.
Ah alright thank you very much! That is annoying duhh..
If I recall, when you conduct a build with Code::Blocks, there is an output window that effectively lists all the command being executed. You could look in that and see if it is (or is not) compiling and linking katt4.cpp
Topic archived. No new replies allowed.