Undefined reference C++? [duplicate] - c++

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.

Related

Linking problems with g++ and clang [duplicate]

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...

Undefined reference to function using Class header [duplicate]

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

C++ Linker Error. Undefined reference to Class::Function() [duplicate]

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!

Undefined reference to sample void function [duplicate]

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

I'm having problems prototyping in .h files in C++ and defining them on a Cpp file [duplicate]

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.