Undefined Reference

I'm a college student majoring in Computer Science but i'm fairly new to C++. While trying to call a function in another class from main, i get the error Undefined reference to Class::Function() error message. Here's the code


#ifndef DISPLAY_H
#define DISPLAY_H

class Display
{
public:
void IntroMessage();
void SectionOne();
void SectionTwo();
void SectionThree();
};

#endif // DISPLAY_H

//Display.cpp
#include "Display.h"
#include <iostream>

using namespace std;

void IntroMessage(){

cout << "Welcome to the Census Program! Please Answer the following questions honestly. :) \n";
}
void SectionOne(){

cout << "Section 1: Personal \n";
}

//Main
#include "Display.h"
#include <iostream>

using namespace std;

Display display;

int main()
{
display.IntroMessage();
display.SectionOne();
return 0;
}

//ErrorMessage

C:\Users\Derv\Desktop\Programs\C++\Census\main.cpp|14|undefined reference to `Display::IntroMessage()'|
C:\Users\Derv\Desktop\Programs\C++\Census\main.cpp|15|undefined reference to `Display::SectionOne()'|


Can someone please explain what is causing the Undefined Reference error?
You implemented funtions in the global namespace, not as a part of class
1
2
3
4
5
6
7
8
void Display::IntroMessage(){ // notice  "Display::"

cout << "Welcome to the Census Program! Please Answer the following questions honestly. :) \n";
}
void Display::SectionOne(){

cout << "Section 1: Personal \n";
}

should work.
Last edited on
In addition to what Null just wrote above, try to use the code tags available next to the text editor when posting codes, it makes them more legible. If you had used the formatting tool for source code, line referencing would have made highlighting the problem easier.

Another error is coming from the declaration statement

Display display

that appears outside of the scope of function main where you called your class's member functions. You have to drag that statement within the braces that define function main.
Last edited on
In your display.cpp file, you need to use the scope resolution operator on your functions to show that they're in the Display class:

1
2
3
4
5
6
7
8
9
10
11
// display.cpp

void Display::IntroMessage()
{
   // Your code here
}

void Display::SectionOne()
{
   // Your code here
}
Please use code tags when posting source code. Your code will be easier to read.

[code]const int i = 0; // example [/code]

Regarding your question, you did not define your member functions.
Instead you defined "free" functions which have the same name, but aren't part of the class.

You can use two styles to define member functions:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// Example.h

#pragma once // an alternative to header guards

#include <iostream>

class Example
{
public:

    // we define the function in the body of the class, no Example.cpp needed
    int say_hello() const
    {
        std::cout << "Hello, World!" << std::endl;
        return 10;
    }
};


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

#pragma once // an alternative to header guards

class Example
{
public:

    // we only declare the function in the body of the class
    int say_hello() const;
};

// ----------------------------------------------------------------

// Example.cpp

#include <iostream>

// definition of the member function
int Example::say_hello() const
{
    std::cout << "Hello, World!" << std::endl;
    return 10;
}


The second style is closer to what you wanted.

Edit: ninja'd but not deleting post.
Last edited on
Ah.. thanks for all the help guys! It workes ^^ But, can i ask why i need to put void Display::IntroMessage ? Isn't it already in the class Display since it's in the cpp file? Sorry if i sound dumb x.x
The compiler doesn't care where functions are defined, it doesn't even pay attention to the names of the source files.
geeloso wrote:
Another error is coming from the declaration statement

Display display

that appears outside of the scope of function main where you called your class's member functions. You have to drag that statement within the braces that define function main.

In this case, display is a global variable, so there is no error.

However, it is generally a good advice to avoid using global variables. This is because it's hard to keep track which functions access them.

Thus (for different reasons) I second the moving of display into main(), unless there's a good reason not to (i.e. functions other than main() must also access it).

But, can i ask why i need to put void Display::IntroMessage ?

As LB already said, the names of the files don't matter in C++.

So if you simply define an IntroMessage() function at global scope, it will be a non-member function (that means, not part of the Display class, or any other class).
Ohh.. thanks :) And i fixed the global variable thing.. are there any other tips i should note when dealing with classes and header files? I only learned this a few hours ago
Last edited on
Ohh.. thanks :) And i fixed the global variable thing.. are there any other tips i should note when dealing with glasses and header files? I only learned this a few hours ago

I recommend reading this site's C++ tutorial, just in case there are things you missed.
http://www.cplusplus.com/doc/tutorial/
http://www.cplusplus.com/doc/tutorial/classes/

As for tips:
1) member functions that don't change member data should be marked as const functions
2) unlearn using namespace std; especially in header files
3) (my personal preference) use #pragma once instead of header guards
4) (my personal preference) define the member functions in-class in the header, rather than in a .cpp file

The points marked with (my personal preference) may or may not be suitable to your needs, and so it's up to you to decide if you follow that way or not.

Read these for:
1) http://www.parashift.com/c++-faq/const-member-fns.html
2) http://www.parashift.com/c++-faq/using-namespace-std.html
3) http://en.wikipedia.org/wiki/Pragma_once
4) http://stackoverflow.com/questions/4837267/member-function-definition
Thanks :) Will see what i can learn :D
4) (my personal preference) define the member functions in-class in the header, rather than in a .cpp file

I appreciate that you've said this is a matter of personal preference. I can think of one very solid, pragmatic reason not to do it that way: if your method definitions are in the header file, then every file that includes that header file becomes dependent on the implementation of those methods. Every time you need to modify the implementation of a method, you'll need to recompile every single file that includes that header.

Whereas, if your method definitions are in a .cpp file, all you need to do is recompile that one cpp file.

Also, if your method definitions are in the header file, then you'll very likely have to include more other header files in that header. Which, again, increases the dependencies of your header, leading to bigger compile times.

Once you start working on projects of a significant size, this stuff can become a major headache. You soon start to see the very real benefits of putting method definitions in a .cpp, not a .h, that go beyond any personal aesthetic considerations.
@ MikeyBoy: yes, and to add to what you wrote, the implementation will be readily available to the users (and there are cases when only the interface in a header file should be visible).

When I wrote the fourth point, I was thinking more in terms of homework assignments. I should have made it clearer that this is not a universally good approach, and that I didn't think it was.
Last edited on
Ah.. okay, but i got another question. Whenever i input information into a string, it only stores the first block of code into the variable. For instance, if i wanted a user to enter his/her address, the variable only contains the first word. Why does this happen and how can i fix it?

Not sure if i'm allowed to ask thsi question here as it's off-topic though.. Can i? as it relates to the same assignment
You're probably using std::cin >> to assign the value to the string.

Use getline instead.

http://www.cplusplus.com/reference/string/string/getline/
Last edited on
Not sure if i'm allowed to ask thsi question here as it's off-topic though.. Can i? as it relates to the same assignment

If the new question doesn't really relate to the original topic, I think it is better to start a new thread specifically for the new question, rather than clutter the existing one (which you started for another reason).

This is because many people that could help may not get to see your new question if the thread title is about something else. Anyway...

Whenever i input information into a string, it only stores the first block of code into the variable. For instance, if i wanted a user to enter his/her address, the variable only contains the first word. Why does this happen and how can i fix it?

The space is considered a delimiter. So a string such as "hello world" will be split up into "hello" and "world", with the latter being kept in the stream buffer.

The solution to this is to use std::getline() which by default considers the delimiter to be newline. I assume that you are using std::string, otherwise go for std::istream::getline().

http://www.cplusplus.com/reference/string/string/getline/
http://www.cplusplus.com/reference/istream/istream/getline/
Last edited on
Thanks again ^^
Topic archived. No new replies allowed.