undefined reference to `saving::rate' [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)
static variable link error [duplicate]
(2 answers)
Closed 5 years ago.
I am write this code but when i compile this code with g++ in arch linux i recive this error
/tmp/ccG7axw1.o: In function `saving::calculate()':
saving.cpp:(.text+0x3a): undefined reference to `saving::rate'
/tmp/ccG7axw1.o: In function `saving::modify()':
saving.cpp:(.text+0x93): undefined reference to `saving::rate'
collect2: error: ld returned 1 exit status
saving.h
class saving{
private :
double savebal;
public :
saving(double newSavebal);
double calculate();
void modify();
static double rate;
};
saving.cpp
#include<iostream>
#include"saving.h"
using namespace std;
saving :: saving(double newSavebal){
savebal = newSavebal;
}
double saving :: calculate(){
savebal += (savebal * (rate / 100))/12;
}
void saving :: modify(){
cout<<"Please enter the new rate"<<endl;
cin>>rate;
}
mainSaving.cpp
#include<iostream>
#include"saving.h"
using namespace std;
void menu(saving );
int main(){
saving s1(500);
menu(s1);
}
void menu(saving s){
int m;
cout<<"1) calculate month interest\n";
cout<<"2) change rate of interest\n";
cin>>m;
switch(m){
case 1 :
s.calculate();
break;
case 2 :
s.modify();
break;
}
}

In Saving.h, you declared the static variable:
static double rate;
But you still need to define it (in other words instantiate it). To do so you should add this to Saving.cpp:
double saving::rate = 0;
Without that, the linker cannot find the actual variable, so any reference to it will result in a linker error.

Because saving is a static member, you have to have it initialized beforehand.
In your saving.cpp file, add this line after including all the headers:
double saving::rate = 0
Your code should then like this:
#include<iostream>
#include"saving.h"
using namespace std;
double saving::rate = 0; //With this line here
saving :: saving(double newSavebal){
savebal = newSavebal;
}

Related

Linking error in c++ (multiple cpp files) [duplicate]

This question already has answers here:
Why can templates only be implemented in the header file?
(17 answers)
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I'm making a prime generator with 3 file (two of them are .cpp and one is .h).
However when i try to build the whole project it in onlinegdb gives this error
/tmp/ccI5GCGK.o: In function `main':
main.cpp:(.text+0x11f): undefined reference to `int primegen(int&, long*, long*)'
collect2: error: ld returned 1 exit status
main.cpp
#include <iostream>
#include "primegen.h"
int main(void)
{
//taking input for number of test cases
int test_case{2};
long int lower_lim[MAX] = {5, 15}, upper_lim[MAX] = {15, 25};
//function present in primefunc.cpp
primegen(test_case,lower_lim,upper_lim);
}
primefunc.cpp
// to make SUCCESS known to this file
extern int SUCCESS;
//main function to prime generator between limits
int primegen(int &test, auto *low, auto *up)
{
static int cases=0;
if(cases == test)
return SUCCESS;
int diff=up[cases]-low[cases];
for(int i=0;i<diff;i++)
{
//some code to be added
}
}
primegen.h
// for making arrays of lower and upper limit
constexpr int MAX = 10;
constexpr int SUCCESS = 2;
// for printing out prime number
int primegen(int &, auto *, auto *);
EDIT :- I tried moving the function from the 2nd cpp to the main.cpp and it worked and also individual builds also gives success.

Compiler gives error about undefined reference to a 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 5 years ago.
Following is my code compiler says undefined reference to function . please elaborate what to do. Why does it give an error about undefined reference to the function isPalindrome() which is boolean?
int main()
{
cout << "Please enter a string: ";
cin >> input;
cout<<"Vowel Count"<<vowelcount(input);
while(1)
{
if(isPalindrome())
{
palindrome_count++;
}
}
cout<<"palindrome_count"<<palindrome_count;
}
bool isPalindrome(string input)
{
do{
if (input == string(input.rbegin(), input.rend())) {
cout << input << " is a palindrome\n";
}
}
while(1);
}
The error message is telling you exactly what you need to know.
IsPalindrome isn't defined in your code before you use it. Make sure to define it above where you reference it (i.e above main) or prototype the function.
I think, you have forgot function declaration. So, put the forword declaration above main() function. Like:
bool isPalindrome(string input);

Undefined reference when using a function included in a header 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 5 years ago.
I am experiencing something weird with my c++ source file or perhaps the compiler itself. It seems that when I attempt to compile the file, it hits me with a message -
undefined reference to "Basic_int_stack::Basic_int_stack()
undefined reference to "Basic_int_stack::Push(int)
Here is my code (I'm still a beginner so don't expect any crazy professional code )
Header file:
class Basic_int_stack
{
public:
// This part should be implementation independent.
Basic_int_stack(); // constructor
void push( int item );
int pop();
int top();
int size();
bool empty();
private:
// This part is implementation-dependant.
static const int capacity = 10 ; // the array size
int A[capacity] ; // the array.
int top_index ; // this will index the top of the stack in the array
};
Implementations:
#include "basic_int_stack.h"// contains the declarations of the variables and functions.
Basic_int_stack::Basic_int_stack(){
// the default constructor intitializes the private variables.
top_index = -1; // top_index == -1 indicates the stack is empty.
}
void Basic_int_stack::push( int item ){
top_index = top_index + 1;
A[top_index] = item ;
}
int Basic_int_stack::top(){
return A[top_index];
}
int Basic_int_stack::pop(){
top_index = top_index - 1 ;
return A[ top_index + 1 ];
}
bool Basic_int_stack::empty(){
return top_index == -1 ;
}
int Basic_int_stack::size(){
return top_index;
}
Main Function:
#include "basic_int_stack.h"
#include <iostream>
int main()
{
int var;
Basic_int_stack s1;
while((std::cin >> var)>=0){
s1.push(var);
}
return 0;
}
This is happening because you're building your main file without building and linking your class implementation file as well. You need to adjust your build settings somehow.
It is because you don't include Basic_int_stack.cpp when you complile.
Simplely speaking, when you encounter Undefined reference to xxx, it is a error generated by linker, when means the compliler can't find the implements. So you need check if you include the cpp file or dynamic library or static library.
I faced the same problem. Finally I found a fix by including .cpp file in the main file.
#include "file_name.cpp" //In the main file

Link error "ld: fatal: Symbol referencing errors." [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 still learing c++ and was following an example from my book. I basically copied their code and added the include and namespace. What am I doing wrong?
Code:
#include <iostream>
#include <string>
using namespace std;
class Date{
int y,m,d;
public:
Date(int y, int m, int d);
int month(){return m;}
int day(){return d;}
int year(){return y;}
};
int main(){
Date b{1970,12,30};
cout<< b.month()<< '\n';
}
Trying to compile with g++ -std=c++11 -o test2 test2.cc
Error:
Date::Date(int, int, int) /var/tmp//ccGuivAs.o
ld: fatal: Symbol referencing errors. No output written to main
collect2: ld returned 1 exit status
Date(int y, int m, int d);
The error message is signaling (in an admittedly unclear way) that there's no definition for Date. It's declared, but not defined. You didn't specify what the constructor does.
Date(int y, int m, int d) {
this->y = y;
this->m = m;
this->d = d;
}
Or, better, using initializer list syntax:
Date(int y, int m, int d): y(y), m(m), d(d) { }
You have to add implementation (definition) of constructor
Date(int y, int m, int d);
At the moment there is only a declaration found in your Date class and such a situation results in
undefined reference to `Date::Date(int, int, int)' collect2: error: ld
returned 1 exit status
http://ideone.com/wMgbKX
You declared the Date::Date constructor, but never defined it.
Your declaration is a promise to the compiler that the constructor Date::Date will be defined somewhere. But you never actually provided a definition. That is what's causing the error.
You can provide a definition right there, inside the class definition, like you did with other member functions. Or you can provide a definition outside the class. It is up to you. But a definition has to be provided somewhere.
You need to implement Date::Date(int, int, int) (i.e. the Date constructor) somewhere, as explicitly stated by your compiler.
You could do that by adding a body to it, like for its month, day and year methods, or outside of the class.

facing error in linking codes in c++ [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.
In C++ I am not able to link source code file and its header files. I am keeping both files in same folder/directory. Also I am using another class which imports header file and it is the startng point of the application but when I am compiling I am getting following error message:
C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o Marksheet_Test.cpp:(.text+0x74):
undefined reference to `Marksheet::Marksheet(std::string,
std::string)'
C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o Marksheet_Test.cpp:(.text+0xa9):
undefined reference to `Marksheet::dispmessage()'
e:\education\dev-cpp\mingw32\mingw32\bin\ld.exe C:\Users\SONY-V~1\AppData\Local\Temp\ccetXYNN.o:
bad reloc address 0x13 in section
`.text$_ZN9MarksheetD1Ev[__ZN9MarksheetD1Ev]'
e:\education\dev-cpp\mingw32\mingw32\bin\ld.exe final link failed:
Invalid operation
E:\Education\C++ programming\collect2.exe [Error] ld returned 1 exit
status
Here Marksheet is a cpp file of which header I am making and Marksheet_Test is starting point of the application.
Can somebody help me solving this problem?
Code is as follows:
This is code for Marksheet_Test
#include "Marksheet.h"
using namespace std;
int main()
{
Marksheet obj1("Pransanjeet Majumder","IT 114 Objject Oriented programming");
obj1.dispmessage();
}
Following code is of Marksheet.cpp
#include<iostream>
#include "Marksheet.h"
using namespace std;
class Marksheet{
Marksheet::Marksheet(string cname,string instname){
setCoursename(cname);
setinstname(instname);
}
void Marksheet::setCoursename(string cname)
{
coursename=cname;
}
void Marksheet::setinstname(string insname){
instname=insname;
}
string Marksheet::getCoursename()
{
return coursename;
}
string Marksheet::getinstname()
{
return instname;
}
void Marksheet::dispmessage()
{
cout<<"Welcome to the "<<coursename<<"\n";
cout<<"This course is offered by Prof."<<instname<<endl;
}
};
Following code is of Marksheet.h header file
#include<string>
using namespace std;
class Marksheet
{
public:
Marksheet(string,string);
void setCoursename(string);
string getCoursename();
void dispmessage();
void setinstname(string);
string getinstname();
private:
string coursename;
string instname;
};
I am using DEVC++ compiler for compiling the code
You have a class Marksheet around your implementations that is unnecessary.
Change Marksheet.cpp to:
#include<iostream>
#include "Marksheet.h"
using namespace std;
Marksheet::Marksheet(string cname,string instname) {
setCoursename(cname);
setinstname(instname);
}
void Marksheet::setCoursename(string cname) {
coursename=cname;
}
void Marksheet::setinstname(string insname) {
instname=insname;
}
string Marksheet::getCoursename() {
return coursename;
}
string Marksheet::getinstname() {
return instname;
}
void Marksheet::dispmessage() {
cout<<"Welcome to the "<<coursename<<"\n";
cout<<"This course is offered by Prof."<<instname<<endl;
}
Note that there is no class in the definition file.
What you were doing was declaring a new class called Marksheet and then attempted to define it's own members without declaring them. Also your should not put you using declarations in the header files as then any class that includes them will also have to use the same declaration. This can lead to hard to find conflicts at compile time.