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.
Account.h file
#ifndef ACCOUNT_H
#define ACCOUNT_H
class Account
{
private:
//attributes
double balance;
public:
//methods
void set_balance(double bal);
double get_balance();
};
#endif
Account.cpp file
#include "Account.h"
void Account::set_balance(double bal){
balance=bal;
}
double Account::get_balance(){
return balance;
}
main.cpp file
#include<iostream>
#include"Account.h"
int main(){
Account frank_account;
frank_account.set_balance(200.00);
double showBalance=frank_account.get_balance();
return 0;
}
error
C:\Users\Dell\AppData\Local\Temp\ccZOi0ua.o: In function `main':
F:/OPP/opp10/main.cpp:6: undefined reference to `Account::set_balance(double)'
F:/OPP/opp10/main.cpp:7: undefined reference to `Account::get_balance()'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
The terminal process terminated with exit code: -1.
Please explain the error message and provide the solution.
Thank You....................................................................................................................
How did you compile these two files? This error suggests that the "Account.cpp" is not compiled or linked by you. Try this:
gcc Account.cpp main.cpp
Related
I am currently learning C++ and was learning how to deal with multiple files when I came across this compiler error:
" In function main':
bravo.cpp:(.text+0x71): undefined reference tosum(int, int)'
collect2: error: ld returned 1 exit status "
The code i was building was:
main function
#include<iostream>
#include "add.h"
int main(){
int a,b;
std::cout<<"Give me two numbers to add: \n";
//std::cin>>a>>b;
std::cout<<"The sum of two numbers is "<< sum(10,5)<< std::endl;
}
header file
#pragma once
int sum(int a, int b);
Sum function
#include<iostream>
#include "add.h"
int sum(int a, int b){
return a+b;
}
So where did i go wrong?
I tried checking the error on stack overflow but i wasn't able to implement the solution on my problem?
Any ideas?
Thanks.
You are receiving a linker error because you likely just tried to compile your main.cpp function. The linker can then not find the sum(int, int), since you do not compile the add.cpp function as well.
Assuming you are using g++, you can compile both files like this:
g++ main.cpp add.cpp
and it will work.
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 have a problem!
It shows this error:
"C:\Users\User\AppData\Roaming\JetBrains\CLion 2018.2.4\bin\cmake\win\bin\cmake.exe" --build C:\Users\User\CLionProject\HospitalTest\cmake-build-debug --target HospitalTest -- -j 2
[ 50%] Linking CXX executable HospitalTest.exe
CMakeFiles\HospitalTest.dir/objects.a(main.cpp.obj): In function `main':
C:/Users/User/CLionProject/HospitalTest/main.cpp:9: undefined reference to `CHospitalWard::CHospitalWard()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:23: undefined reference to `CHospitalWard::OnAdd()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:24: undefined reference to `CHospitalWard::OnDelRegNum()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:25: undefined reference to `CHospitalWard::OldestPatient()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:26: undefined reference to `CHospitalWard::OnPrint()'
C:/Users/User/CLionProject/HospitalTest/main.cpp:27: undefined reference to `CHospitalWard::IsInRegNum()'
collect2.exe: error: ld returned 1 exit status
mingw32-make.exe[3]: *** [CMakeFiles\HospitalTest.dir\build.make:85: HospitalTest.exe] Error 1
mingw32-make.exe[2]: *** [CMakeFiles\Makefile2:72: CMakeFiles/HospitalTest.dir/all] Error 2
mingw32-make.exe[1]: *** [CMakeFiles\Makefile2:84: CMakeFiles/HospitalTest.dir/rule] Error 2
mingw32-make.exe: *** [Makefile:117: HospitalTest] Error 2
Code:
///////////////////////////////
#pragma once
#include "Patient.h"
#include <string>
using namespace std;
class CHospitalWard
{
private:
string m_name;
int m_br;
CPatient *m;
public:
CHospitalWard();
CHospitalWard(string, int);
~CHospitalWard(){delete []m;}
void OnAdd();
void OnDelRegNum();
void OnPrint();
int IsInRegNum();
void OldestPatient();
string name_access() {return m_name;}
int br_access() {return m_br;}
};
There are declared like this:
cout<<"Generating..."<<endl;
CHospitalWard f;
int c;
string s;
It is possible that you have included this file, but you have not yet written implementations for these functions. You will need to implement all functions in order for the program to compile.
The minimum implementation for function void DoNothing(); for example would be,
void DoNothing() {}
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
static variable link error [duplicate]
(2 answers)
Closed 3 years ago.
I am not able to use structures for static variable and function. Could anyone explain why exactly is this behavior?
#include <iostream>
using namespace std;
class Foo
{
private:
typedef struct
{
int bee_1;
}bee;
static bee test;
public:
static void Inc(){ test.bee_1++;}
static int getBee_1(){return test.bee_1;}
};
int main(){
Foo::Inc();
Foo::Inc();
Foo::Inc();
Foo::Inc();
cout << Foo::getBee_1();
return 0;
}
I get the following error when I use this code:
clang++-7 -pthread -o main main.cpp
/tmp/main-521333.o: In function `Foo::Inc()':
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x24): undefined reference to `Foo::test'
main.cpp:(.text._ZN3Foo3IncEv[_ZN3Foo3IncEv]+0x2e): undefined reference to `Foo::test'
/tmp/main-521333.o: In function `Foo::getBee_1()':
main.cpp:(.text._ZN3Foo8getBee_1Ev[_ZN3Foo8getBee_1Ev]+0x7): undefined reference to `Foo::test'
clang: error: linker command failed with exit code 1 (use -v to see invocation)
compiler exit status 1
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 used the following:
gcc -c -O4 ab_test.c
This worked and generated ab_test.o without error, but
gcc -o ab_test ab_test.o -lgsl -lgslcblas -lm
lead to error as:
**/usr/lib64/gcc/x86_64-suse-linux/4.8/../../../../lib64/crt1.o: In function `_start':
/home/abuild/rpmbuild/BUILD/glibc-2.18/csu/../sysdeps/x86_64/start.S:118: undefined reference to `main'
collect2: error: ld returned 1 exit status**
The code is ab_test.c is as under
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <gsl/gsl_rng.h>
#include <gsl/gsl_randist.h>
Main()
{
int i, temp_int;
char amode[30];
char bmode[30];
float wave_vector_y;
for(i=0; i<41; i++)
{
//// set wave vector ////
wave_vector_y = i*0.005;
temp_int = 10000*wave_vector_y;
sprintf(amode,"a%04d.dat",temp_int);
sprintf(bmode,"b%04d.dat",temp_int);
}
}
Your "main" signature should be something like int main(void)or int main(int argc ,char *argv[]) and not old C style syntax for int Main().
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 have recently made a little example in order to practice separating C++ code in .h and .cpp files using Geany. The code compiles without issue, but the following error occurs when I build:
g++ -std=c++11 -Wall -o "parent1" "parent1.cpp" (in directory: C:\Program Files (x86)\Geany)
C:\Users\Sabine\AppData\Local\Temp\ccnonGdW.o:parent1.cpp:(.text+0x15): undefined reference to `grandparent::grandparent(int)'
c:/mingw/bin/../lib/gcc/mingw32/4.8.1/../../../../mingw32/lib/libmingw32.a(main.o): In function `main':
i:\p\giaw\src\pkg\mingwrt-4.0.2-1-mingw32-src\bld/../mingwrt-4.0.2-1-mingw32-src/src/libcrt/crt/main.c:91: undefined reference to `WinMain#16'
collect2.exe: error: ld returned 1 exit status
Compilation failed.
The source files:
parent1.h:
#ifndef PARENT1_H
#define PARENT1_H
#include "grandparent.h"
class parent1 :public grandparent
{ private:
int i ;
public:
parent1(int p1a, int p1b, int p1c);
};
#endif
parent1.cpp:
#include "parent1.h"
#include "grandparent.h"
#include <iostream>
#include <string>
using namespace std;
parent1::parent1(int a, int b, int c)
: grandparent(a)
{}
Hi thanks for the quick replies.
Remove this line #include "grandparent.h" in parent1.cpp --- that didn`t work. ( Error: expected class-name before {-token )
grandparent.h looks like this:
#ifndef GRANDPARENT_H
#define GRANDPARENT_H
class grandparent
{ private:
int gx;
public:
grandparent(int gx);
};
#endif
You need to link with the grandparent object - grandparent.o or the library it belongs to.
UPDATE: In particular you need (assuming you've already compiled grandparent.cpp):
g++ -std=c++11 -Wall -o "parent1" "parent1.cpp" "grandparent.o"
I believe that
g++ -std=c++11 -Wall -o "parent1" "parent1.cpp" "grandparent.cpp"
will also work.