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
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 1 year ago.
I am writing a simple program to compile from the command prompt. There is an app1 program that uses a class located in a separate file. The class sample1 has a header file sample1.h where all the function declarations exist. The sample1.cpp file contains the function definitions.
app1.cpp:
#include <iostream>
#include "sample1.h"
using namespace std;
int main()
{
sample1 t;
cout<<"The program is ending"<<endl;
return 0;
}
sample1.h:
#ifndef SAMPLE1_H
#define SAMPLE1_H
class sample1
{
public:
sample1();
};
#endif
sample1.cpp:
#include<sample1.h>
#include <iostream>
using namespace std;
sample1::sample1()
{
cout<<"This error sucks"<<endl;
}
I am compiling this program using Windows Subsystem for Linux. However, when I try to compile with g++ 'app1.cpp' -o app1, I get this error:
I get that the error is telling me that the program cannot find the constructor sample::sample() but why is that? I included the header file in the program. These 3 files all exist in the same folder.
You have to compile all your .cpp files (sample1.h is included by preprocessor from information in app1.cpp only):
g++ app1.cpp sample1.cpp -o app1
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:
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
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.