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!
Related
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 1 year ago.
I am sure this question has be asked and answered million times but I am really clueless. As a newbie to C++ I have created three textbook files.
Num.h
#ifndef LIB_DATE_H
#define LIB_DATE_H
class Num
{
private:
int num;
public:
Num(int n);
int getNum();
};
#endif
Num.cpp
#include "Num.h"
Num::Num(int n) : num(n) {}
int Num::getNum()
{
return num;
}
main.cpp
#include <iostream>
#include "Num.h"
using namespace std;
int main()
{
Num n(35);
cout << n.getNum() << endl;
return 0;
}
Both g++ and clang complain about undefined reference to Num::Num(int) and Num::getNum().
When joining the three files into a single one, no errors are reported by g++; clang is still complaining.
I have managed to google out that this is a problem with linking but I was not able to find any actionable advice to fix it...
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)
Closed 3 years ago.
I'm learning C++ following this link: https://www.youtube.com/watch?v=mUQZ1qmKlLY. Now I'm confused about member initialization. So, I have three files, main.cpp, Sally.h, Sally.cpp. as the following.
main.cpp
#include <iostream>
#include "Sally.h"
using namespace std;
int main(){
Sally so(3,87);
so.print();
}
Sally.h
#ifndef SALLY_H
#define SALLY_H
class Sally
{
public:
Sally(int a, int b);
void print();
private:
int regVar;
const int constVar;
};
#endif
Sally.cpp
#include "Sally.h"
#include <iostream>
using namespace std;
Sally::Sally(int a, int b)
:regVar(a), constVar(b)
{
}
void Sally::print()
{ cout << "regulat var is: " << regVar << "const var is:" << constVar << endl;
}
When I run the main.cpp file, it does not give me any prints out. Instead, it gives me the following message.
$ g++ main.cpp
/tmp/ccyvg9rV.o: In function `main':
main.cpp:(.text+0x29): undefined reference to `Sally::Sally(int, int)'
main.cpp:(.text+0x35): undefined reference to `Sally::print()'
collect2: error: ld returned 1 exit status
Moreover, why do not I see anything similar to this member initialization in other languages, like Java, Julia or Python?
When I run the main.cpp file
g++ main.cpp
That does not run the file, it compiles and links it.
Since main.cpp is not a full program (you also need Sally.cpp), the linker tells you you have undefined references.
Instead, do:
g++ Sally.cpp main.cpp
And you should get a binary/executable in your current folder that you can run.
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 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.