Undefined Reference to 'Function'

Help! Whenever I try to Build and Run this code in C::B, it gives me an error called Undefined Reference to 'levelOne()':


main.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
  #include <iostream>

#include "hi.h"

using namespace std;

int main()
{
    cout << "Hello world!" << endl;
    levelOne();
    return 0;
}


hi.cpp:
1
2
3
4
5
6
#include "hi.h"

bool levelOne() {
    cout << "HI"<<endl;
    return true;
}

hi.h:
1
2
3
4
5
6
7
8
#include <iostream>

#ifndef HI_H_INCLUDED
#define HI_H_INCLUDED

bool levelOne();

#endif // HI_H_INCLUDED 
Last edited on
You're getting a linker error, so did you add hi.cpp to your project?

Sorry, but how do i do that?
Right click on your project in the Management Window, then select add file and follow the prompts.

It is in the project, do i have to do something in linker settings?
Probably not, just build the project and see if your error messages are fixed.

Hmm well CB should do this kinda stuff for you, but Project->Properties->Build Targets "build target files" box, can you see a list of all the files you need there? And they're checked?
Ok, that worked. But now I am getting a new error. Now it is saying that cout was not declared in this scope in the file hi.cpp. I tried to add #include <iostream> and using namespace std; to it and it worked. Do I have to do this every time i want to create another cpp file, because I was told that I only have to do #include <iostream> and using namespace std; in the cpp file that has main() and also the headers

EDIT: Sorry, the error was because i didn't declare that I was using the standard namespace in the header. Never mind my last question, thanks for the help
Last edited on
Do I have to do this every time i want to create another cpp file


Yes. If you use something from the standard namespace you need to properly scope the standard namespace. Another option is to properly scope the std namespace using the scope resolution operator::

std::cout << "This is an example of scoping the standard namespace" << std::endl;
This will need to be done where ever you use something that is in the standard namespace. And many people, myself included recommend this practice.

Sorry, the error was because i didn't declare that I was using the standard namespace in the header

You should never use a using statement in the global scope in a header file. Doing so is considered a very very bad practice.
Topic archived. No new replies allowed.