This question already has answers here:
C++ error 'Undefined reference to Class::Function()' [duplicate]
(4 answers)
Closed 8 months ago.
After searching for a solution to this for about a half an hour I have made no progress.
The errors are as follows:
s\My Workspace\Project\main.cpp - Line 7 - undefined reference to 'Sally::Sally()'
s\My Workspace\Project\main.cpp - Line 9 - undefined reference to 'Sally::printCrap()'
main.cpp
#include <iostream>
using namespace std;
#include "Sally.h"
int main()
{
Sally sallyObject;
sallyObject.printCrap();
}
Sally.h
#ifndef SALLY_H
#define SALLY_H
class Sally
{
public:
Sally();
void printCrap();
};
#endif // SALLY_H
Sally.cpp
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally(){
}
void Sally::printCrap(){
cout << "Did someone say steak?" << endl;
}
Thank you in advance!
I know this is a pretty old question but maybe it could help someone.
So, when adding any additional files (headers, source, etc.) follow this (if using Eclipse or similar IDE):
New file -> File... -> C/C++ header (source, etc.) -> next, next -> give it a name and make sure it's in the same path with your project, then check "Add file to active project", in build target(s): check all -> Finish.
Hope it helps.
Your linker doesn't find Sally.cpp. (Quick intro to linker)
To compile your code type:
g++ -o main main.cpp Sally.cpp
Related
This question already has answers here:
Code-runner configuration for running multiple cpp classes in vscode
(1 answer)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I've been following tutorial on how to create class using header files and came to a problem even if I did everything like in tutorial. I got Cat.h, Cat.cpp and main.cpp files. All of them are in the same folder.
Cat.h:
#ifndef CAT_H_
#define CAT_H_
class Cat
{
public:
void speak();
};
#endif
Cat.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
void Cat::speak()
{
cout << "Meeeow!" << endl;
}
main.cpp:
#include <iostream>
#include "Cat.h"
using namespace std;
int main()
{
Cat jim;
jim.speak();
return 0;
}
When i run the program I got error: "undefined reference to `Cat::speak()'". The problem is solved when i add line #include "Cat.cpp" to main.cpp but I dont think thats a way to go and tutorial was done without that.
Solved. For anyone having the same problem using VS Code with Code Runner extension, I found the solution in different thread:
Code-runner configuration for running multiple cpp classes in vscode
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
How does the compilation/linking process work?
(5 answers)
Closed 2 years ago.
I am new to C++ and I am creating a very simple program using classes. I am using Visual Studio Code to write my code and MinGW compiler to build and run it.
I have written the following code as an example to show my problem.
Main.cpp
#include "class.h"
using namespace std;
int main(void) {
myClass myObject;
myObject.sum(10, 20);
return 0;
}
class.h
#pragma once
class myClass{
public:
void sum(int a, int b);
};
class.cpp
#include <iostream>
#include "class.h"
using namespace std;
void myClass::sum(int a, int b) {
cout << "The sum = " << a + b << endl;
}
The compiler is giving me this error when I try to build it.
undefined reference to `myClass::sum(int, int)'
collect2.exe: error: ld returned 1 exit status
BTW I know there are a lot of similar questions on the forum and I am sorry I am posting it again but all those solutions didn't seem to work for me as my program here is pretty simple. Any help will be very appreciated. Thanks!
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 6 years ago.
well I want to understand linking with headers and other .cpp (functions for example) so my quastion is why I get "undefined reference to 'afis(). There are sample example and I want to clarify this. Also sorry for my bad english :D.
There is main:
#include <iostream>
#include "functions.h"
using namespace std;
int main()
{
afis();
return 0;
}
There is an function named function.cpp:
#include <iostream>
using namespace std;
void afis(){
cout <<"yehe";
}
And there is the header :
#ifndef FUNCTIONS_H_INCLUDED
#define FUNCTIONS_H_INCLUDED
void afis();
#endif // FUNCTIONS_H_INCLUDED
While the C++ compiler automatically "pulls in" referenced header files, it can't do that for the actual .cpp code files.
Instead of calling
CXX/clang++/g++ main.cpp -o hello
you need to manually include all relevant code files:
CXX/clang++/g++ main.cpp functions.cpp -o hello
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
The project has 2 classes - Tree and TreeTest class
The TreeTest class tests the functions of Tree class.
TreeTest.h
#ifndef TREETEST_H
#define TREETEST_H
class TreeTest
{
public:
TreeTest();
virtual ~TreeTest();
void InitTreeFunctionTest();
protected:
private:
};
#endif // TREETEST_H
TreeTest.cpp
#include "TreeTest.h"
#include "Tree.h"
#include <cstring>
#include <iostream>
using namespace std;
void TreeTest::InitTreeFunctionTest()
{
//code goes here
}
Main.cpp
#include <iostream>
#include <Tree.h>
#include <TreeTest.h>
using namespace std;
int main(int argc, char* argv[])
{
TreeTest* tt;
tt->InitTreeFunctionTest();
}
But it gives the following error when I compile using
g++ -fprofile-arcs -ftest-coverage main.cpp -I<full path to library> -o test
undefined reference to `TreeTest::InitTreeFunctionTest()'
Can anyone please help me find the error?
Thanks
You have not compiled TreeTest.cpp
Try adding it to your command line:
g++ -fprofile-arcs -ftest-coverage TreeTest.cpp main.cpp -I<full path to headers> -o test
edit:
You will also need to provide the definitions of the constructor and destructor in TestTree.cpp
The other solution is to inline the function definition in the header. But, you probably want to compile it unless the functions are trivial.
Please use #include "TreeTest.h", not #include <TreeTest.h>. If you want to use #include <TreeTest.h>, first, you must make your TreeTest.cpp into a static library. BTW, you can search for the difference between #include "" and #include <>. I hope this can help you.
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 8 years ago.
I'm kinda new to stack overflow so let me know how I can improve my format for asking questions and displaying code.
My problem is that
I keep getting a Linker error undefined reference to Father::Father() and undefined reference to Father::display()
Here is my code:
Father.h
#ifndef FATHER_H
#define FATHER_H
#include <iostream>
class Father
{
public:
Father();
void display();
private:
};
#endif
Father.cpp
#include <iostream>
#include "Father.h"
Father::Father
{
}
void Father::display()
{
std::cout << "I am your father" << std::endl;
}
/*
This is my enter code here
*/
Test.cpp
#include <iostream>
#include "Father.h"
int main()
{
Father Darth;
Darth.display();
system("pause");
}
Linking two .cpp and a .h files
I just saw this as a possible solution, I'm thinking I'm having problems with linking H files with CPP files.