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
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";
}
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.
Please use code tags when posting source code. Your code will be easier to read.
[code]constint 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;
}
};
// 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;
}
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
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
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
As for tips: 1) member functions that don't change member data should be marked as const functions 2) unlearn usingnamespace 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.
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.
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
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().