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...
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 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:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 7 years ago.
New to c++, and trying to use a file our professor made for us and I can't use any of the functions it provides heres the code that gives an Undefined reference. I didn't include the function implementations but they again were provided so they are correct. I'm using codeblocks and all three files are in my sources folder. What is causing this?
#include <iostream>
using namespace std;
#include "random.h"
int main()
{
cout<< random_instance(6,2.5,1);
return 0;
}
this is the random.h
#ifndef RANDOM_H
#define RANDOM_H
#define TWO_PI 6.2831853071795864769252866
double * random_instance( int length, const double mu, const double sigma = 1 );
double gaussian_rand( const double mu, const double sigma );
#endif /* defined(RANDOM_H) */
The definition for random_instance is missing.
You should probably compile and link random.cpp too which should contain the definitions for the functions declared in random.h, maybe something like this:
g++ path/to/main.cpp path/to/random.cpp
In Code::Blocks there's probably a section somewhere where you can choose the files to compile and link.
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.