'Undefined reference to' error

Hi, I recently started learning C++ and encountering some errors.
I use Code::Blocks as my IDE.

While trying seperate files, something goes wrong.

This is the main.cpp file
1
2
3
4
5
6
7
8
9
10
11
#include "Abc.h"

using namespace std;


int main()
{
    Abc x;
    x.Abcde();
    return 0;
}


This is the Abc.h file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef ABC_H
#define ABC_H

class Abc
{
    public:
        Abc();
        void Abcde();
    protected:

    private:
};

#endif // ABC_H 


This is the Abc.cpp file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "Abc.h"
#include<iostream>
using namespace std;

Abc::Abc()
{
cout<<"Hi";
}

void Abc::Abcde()
{
    cout<<"Hmm";
}


This is what the compiler shows as error:
error: undefined reference to 'Abc::Abc()'
error: undefined reference to Abc::Abcde()'
error: 1d returned 1 exit status

What is causing the problem and what's the solution?
Last edited on
I don't believe you need a return 0; value in Main at this point.
While trying seperate files, something goes wrong.


Did you add the files to the project in CodeBlocks? The code looks fine. There should be menu options to do that. There should also be options to create a class, if you use it, it will add the files automatically.

@ Aeluwin

Even though an implicit 0 is returned from main when there is no return statement, there is nothing stopping one from returning something anyway.
@TheIdeasMan
How do I add files to the project?
I did use the new class in file menu but maybe I did something wrong.
Well if you used the menu to create the class it should work. Are all 3 files in the same directory? Before, presumably you had all the code in main.cpp, are all the new files in that same directory? I no longer have C::B on my current computer, so I can't give much more help. Anyway linker errors mean that it can't find the definition of the function, which probably means in this case it can't find the files.
Yeh all 3 files are in the same directory.

I'll try repeating the process maybe
Give me about 20 minutes to compile this on Code::Blocks and I'll try to help you out. Looking at the code, there's not really any syntax problems, could just be something with files, or your directory or options in the compiler. I'll try my best friend :)
So, What I made some changes, and I'll point them out here...

Main.cpp
I added "#include <iostream>" and deleted "return 0;"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Your Code
#include "Abc.h"
using namespace std;

int main()
{
	Abc x;
	x.Abcde();
	return 0;
}

//My Code
#include "Abc.h"
#include <iostream>
using namespace std;

int main()
{
	Abc x;
	x.Abcde();
}


Abc.h
Deleted private and protected, since they were unused
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
//Your Code
#ifndef ABC_H
#define ABC_H

class Abc
{
public:
	Abc();
	void Abcde();
protected:

private:
};

#endif // ABC_H 

//My Code
#ifndef ABC_H
#define ABC_H


class Abc
{
public:
	Abc();
	void Abcde();
};

#endif // ABC_H 


Abc.cpp
Added "endl;" to "cout"
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
//Your Code
#include "Abc.h"
#include<iostream>
using namespace std;

Abc::Abc()
{
	cout << "Hi";
}

void Abc::Abcde()
{
	cout << "Hmm";
}

//My Code
#include "Abc.h"
#include <iostream>
using namespace std;

Abc::Abc()
{
	cout << "Hi" << endl;
}
void Abc::Abcde()
{
	cout << "Hmm" << endl;
}


Honestly I'm not sure how my code compiled and worked. I tested it, it ran. Then I tested it by making it into one file, and it ran.. The code in one file is below
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;

class Abc
{
public:
	Abc();
	void Abcde();
};

Abc::Abc()
{
	cout << "Hi" << endl;
}
void Abc::Abcde()
{
	cout << "Hmm" << endl;
}

int main()
{
	Abc x;
	x.Abcde();
}


I'm guessing the "return 0;" messed everything up somehow, or perhaps not adding the #include <iostream>, since before, the program couldn't write into the input/output streams without it. But the definite reason is beyond me.

My guess would be like others are saying, and you didn't properly create your project.
Last edited on
Are you sure you created a project before you started adding files and classes?

Yes
Topic archived. No new replies allowed.