Code::Blocks Undefined reference error

Feb 27, 2012 at 1:05am
Hi everybody

I've been getting a very strange error when I use codeblocks, the "undefined reference error. Now if I was using a library which I hade to link to I would understand why I would get this but I'm just making an incredibly simple test class.

The code:

Test.h:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#ifndef TEST_H_INCLUDED
#define TEST_H_INCLUDED

class test
{
    int a;
    char b;
public:
    void set_int(int i);
    void set_char(char c);
    void show();
};

#endif // TEST_H_INCLUDED 


Test.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include"Test.h"
#include<iostream>

void test::set_int(int i)
{
    a = i;
}

void test::set_char(char c)
{
    b = c;
}

void test::show()
{
    std::cout << a << "\t" << b;
}


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include "Test.h"

//using namespace std;

int main()
{
    std::cout << "Hello world!" << std::endl;

    test t;
    t.set_int(4);
    t.set_char('b');
    t.show();

    return 0;
}


It gives me the following error code:

Undefined reference set to 'test::set_int(int)'
Undefined reference set to 'test::set_char(char)'
Undefined reference set to 'test::show()'

The strangest thing is that this code will compile in visual studio.

Any help will be appreciated :)
Feb 27, 2012 at 1:15am
closed account (zb0S216C)
Try cleaning your project, then rebuilding it. It that fails, create an entirely new project, and include those files.

Wazzak
Feb 27, 2012 at 1:29am
What do you mean with cleaning it?
Feb 27, 2012 at 1:31am
closed account (zb0S216C)
Basically, it removes any invalid data (such as files & cache entries) that once pertained to the project; thus, purging the project of all out-of-data information.

Wazzak
Feb 27, 2012 at 1:31am
Build/Clean.
You have to add files you want to be compiled to your project.
Feb 27, 2012 at 1:39am
Tried it and nothing happened.

I know that creating a new project won't change anything since I've tried this in three other projects with different classes and the same errors keep coming up.

It will however recognize that the class is there because if I change the main function to this:
1
2
3
4
5
6
7
8
9
10
11
int main()
{
    std::cout << "Hello world!" << std::endl;

    test t;
    /*t.set_int(4);
    t.set_char('b');
    t.show();
    */
    return 0;
}

it will work.
Feb 27, 2012 at 1:42am
Of course, if it failed to include the header file, you'd get a preprocessor error.
See:
You have to add files you want to be compiled to your project.
Feb 27, 2012 at 1:44am
Even if they are in the folders of the project?
Feb 27, 2012 at 1:46am
Yes. Always create new files with Code::Blocks, so they get added to your project automatically.
Feb 27, 2012 at 1:48am
But I did create them in codeblocks.

Anyway, how do I add them to the project then?

EDIT: Yay it worked, thanks a lot :)
Last edited on Feb 27, 2012 at 1:56am
Feb 27, 2012 at 1:55am
Project/Add files?!
Feb 27, 2012 at 1:56am
closed account (zb0S216C)
On the workspace pane, on the left-hand side, locate your project. Right-click it an select the Add files... menu item. Code::Blocks will automatically organise your files based on the extension.

Wazzak
Last edited on Feb 27, 2012 at 1:56am
Topic archived. No new replies allowed.