This question already has answers here:
C++ Undefined Reference to vtable and inheritance
(3 answers)
Closed 9 years ago.
I have a interface and I want to create a header with the functions from the interface and a .cpp implementing the functions in this header. But when try this I always get the problem undefined reference to 'vtable for Test' in the Testt.h file.
I'm working on a rather big project in eclipse so I reduced my problem to a few small classes.
ITestAdapter.h:
#ifndef ITESTADAPTER_H_
#define ITESTADAPTER_H_
class TestAdapter {
public:
virtual int test() = 0;
};
#endif /* ITESTADAPTER_H_ */
Testt.h:
#ifndef TESTT_H_
#define TESTT_H_
#include "ITestAdapter.h"
class Test: public TestAdapter{
public:
virtual int test();
};
#endif /* TESTT_H_ */
Testt.cpp:
#include "Testt.h"
int test() {
return 0;
}
Test_main.cpp:
#include <iostream>
#include "Testt.h"
using namespace std;
int main() {
Test t;
int i = t.test();
cout << i << endl;
return 0;
}
If I don't use the Testt.h at all and implement the interface within Testt.cpp and include Testt.cpp (what I want to avoid) within the file with my main-method then it works fine.
Testt.cpp (modified):
#include "ITestAdapter.h"
class Test: public TestAdapter {
public:
int test() {
return 0;
}
};
So I don't understand why it doesn't work if I use a header (which I think would be the nicer solution).
I hope I could explain clearly what my problem is. If not please ask.
You are defining a non-member function int test() in Testt.cpp. You need to define int Test::test():
int Test::test()
{// ^^^^
return 0;
}
Undefined reference to X means that the linker can't find the definition of X that has been declared.
You declared Test to have a member function int test() but this
int test() {
return 0;
}
defines a free function.
You need
int Test::test() {
return 0;
}
undefined reference to 'vtable for Test'" to test is a bit confusing. It usualy means that you forgot to define the first virtual function of your class.
Related
This question already has answers here:
What is an undefined reference/unresolved external symbol error and how do I fix it?
(39 answers)
Closed 2 years ago.
I'm getting undefined reference errors. Can you please figure out why?
Errors:
undefined reference to Breuken::Breuken(int, int)
undefined reference to Breuken::som()
(I'm kinda new in c++ programming. I am currently working on a project now, using header files another source file and main source file.)
Here is my code:
Header File (Breuk.H):
using namespace std;
class Breuken{
Breuken(int, int);
void som();
void setTeller1(int);
void setNoemer1(int);
int getTeller1();
int getNoemer1();
private:
int teller1;
int noemer1;
};
Source File(Breuk.cpp):
#include <iostream>
#include "Breuk.h"
using namespace std;
Breuken::Breuken(int tel1, int noem1)
{
setTeller1(tel1);
setNoemer1(noem1);
}
int Breuken::getTeller1()
{
return teller1;
}
int Breuken::getNoemer1()
{
return noemer1;
}
void Breuken::setTeller1(int tel1)
{
teller1 = tel1;
}
void Breuken::setNoemer1(int noem1)
{
noemer1 = noem1;
}
int Breuken::som()
{
cout<<"Breuken zijn: "<<getTeller1()<<" / "<<getNoemer1()<<endl;
}
main file(main.cpp):
#include <iostream>
#include "Breuk.h"
using namespace std;
int main()
{
Breuken br(1, 2);
br.som();
return 1;
}
If I got this right, you want to use a class, but the only thing you did is declare some functions without actually defining them.
What you need to do is put everything in your header inside a class like that:
class Breuk {
private:
int teller1;
int noemer1;
public:
Breuk(int tel1, int noem1);
void som();
void setTeller1(int tel1);
void setNoemer1(int noem1);
int getTeller1();
int getNoemer1();
};
Also, don't forget to give the variables inside the function head a name!
The .cpp file you need to get the reference to the class right (the part before the ::). It has to be the same name as your class. Also, it's not so great to use setter inside the constructor. It's better to reference the member variables directly (the variables inside the class). In order to do that you need to use "this->variable". "this" will refer to the current class you're in. That means if you write this->teller1 it will refer to the variable teller1 inside the class.
#include <iostream>
#include "Breuk.hpp"
using namespace std;
Breuk::Breuk(int tel1, int noem1)
{
// setTeller1(tel1); theoreticly ok but its better to access
// setNoemer1(noem1); member variables (variables inside a class) directly
this->teller1 = tel1;
this->noemer1 = noem1;
}
int Breuk::getTeller1()
{
return teller1;
}
int Breuk::getNoemer1()
{
return noemer1;
}
void Breuk::setTeller1(int tel1)
{
teller1 = tel1;
}
void Breuk::setNoemer1(int noem1)
{
noemer1 = noem1;
}
void Breuk::som()
{
cout << "Breuken zijn: " << getTeller1() << " / " << getNoemer1() << endl;
}
in the main.cpp you only made a small mistake with the return 0 and the name of the class.
#include "Breuk.hpp"
using namespace std;
int main()
{
Breuk br(1, 2); // make sure u use the same name your class has
br.som();
return 0; // return 0 at the end: it means that the program executed correctly
// return 1 would mean that an error occured
}
Also, I suggest using .hpp files instead of .h files. There is no real difference but it's more common to use .hpp for c++.
I'm trying to run some test code to learn c++, but I am getting an error telling me the reverseDigits function was not declared in the main.cpp scope:
error: 'reverseDigits' was not declared in this scope.
But the #include "Solutions.h" header was included in main.cpp, so I thought that it would be in scope.
I have checkout other questions, but the answers all relate to problems with circular header file inclusion, which I don't think is the problem here.
Do you know why I am seeing that error?
Solution.h
#ifndef SOLUTION_H
#define SOLUTION_H
class Solution {
public:
Solution();
~Solution();
int reverseDigits(int x);
};
#endif // SOLUTION_H
Solution.cpp
#include "Solution.h"
#include <string>
Solution::Solution()
{
}
Solution::~Solution()
{
}
int Solution::reverseDigits(int x) {
std::string num_string = std::to_string(x);
std::string reversed_num_string {};
for (int i = num_string.length() - 1; i > 0; i--) {
reversed_num_string.push_back(num_string[i]);
}
return stoi(reversed_num_string);
}
main.cpp
#include <iostream>
#include "Solution.h"
int main()
{
int x {123};
int result = reverseDigits(x);
std::cout << result << std::endl;
return 0;
}
You declared reverseDigits as a member function of the Solution class, then defined it without qualifying it as a member of Solution (Edit: You've since changed it to match declaration and definition, but at point of use, you're trying to use an unqualified function, not a member of a Solution object). The declaration in the .h file is visible, but the definition in the .cpp is unrelated, and not visible to main.cpp.
Declare the function outside the class (since it's clearly unrelated to the class), and it should work, changing to:
class Solution {
public:
Solution();
~Solution();
};
int reverseDigits(int x); // NOT inside the class definition
I'll note: I have no idea why you have a Solution class at all. Defining reverseDigits doesn't require it, so I'm not seeing the point. If this is part of some automated evaluation framework, you'll have to give more details
Along with ShadowRanger's valid suggestion, I'll highlight upon how you could have used the data as part of your Solution class and applied the function on it.
Refactoring your class to
class Solution {
public:
Solution(int data);
~Solution();
int reverseDigits();
private:
int m_data;
};
Solution::Solution(int data)
{
m_data = data;
}
Solution::~Solution()
{
}
Even though you could have used std::reverse, fixing the error on the i>=0 is needed to have your own reverse function
int Solution::reverseDigits() {
std::string num_string = std::to_string(m_data);
std::string reversed_num_string {};
for (int i = num_string.length() - 1; i >= 0; i--) {
reversed_num_string.push_back(num_string[i]);
}
return stoi(reversed_num_string);
}
Now call it from your main() as
int main() {
int x = 123;
Solution sol(x);
std::cout << sol.reverseDigits() << std::endl;
return 0;
}
This question already has answers here:
Undefined reference C++
(2 answers)
Closed 9 years ago.
I have done some research and googling on this error for days, as far as I know this is a common problem for many in c++, I still haven't found a clear answer to this error. I've read that linking the files can fix this, but I could find any example codes to do this. I'am so close to finishing this code, all I need to do to call the constructor from the main file (or just make a simple object), but I keep getting this "unidentified reference to NamedStorm::NamedStorm()" error, please help.
MAIN.CPP
#include <iostream>
#include <string>
#include "NamedStorm.h"
using namespace std;
NamedStorm storm[2];
int main(){
// NamedStorm Chris("Chris", 70.0, "T.S.", 990.0);
// storm[0] = Chris;
return 0;
}
NamedStorm.cpp
// CPP => Function definition
#include <string>
#include "NamedStorm.h"
using namespace std;
// Defining static variables
int NamedStorm::stormCount = 0;
// Constructor definition
NamedStorm::NamedStorm(std::string sName, double wSpeed, std::string sCat, double sPress){
stormName = sName;
windSpeed = wSpeed;
stormCategory = sCat;
stormPressure = sPress;
stormCount++;
}
NamedStorm::NamedStorm(std::string sName){
stormName = sName;
stormCount++;
}
NamedStorm::NamedStorm(){
stormName = sName;
stormCount++;
}
// Destructor definition
//NamedStorm::~NamedStorm(){}
// Get (Accessor) function definition
int NamedStorm::getStormCount(){
return stormCount;
}
double NamedStorm::getStormPressure(){
return stormPressure;
}
string NamedStorm::getStormCategory(){
return stormCategory;
}
string NamedStorm::getName(){
return stormName;
}
// Set (Mutator) function definition
void NamedStorm::displayOutput(){}
void NamedStorm::sortByNames(){}
void NamedStorm::getAverageStormPressure(){}
void NamedStorm::getAverageWindSpeed(){}
void NamedStorm::getWindSpeed(){}
NamedStorm.h
#ifndef NAMEDSTORM_H_INCLUDED
#define NAMEDSTORM_H_INCLUDED
// NEVER use using namespce in header, use std instead.
class NamedStorm{
private:
std::string stormName;
std::string stormCategory;
double maxWindSpeed;
double stormPressure;
static int stormCount;
public:
// Constructor
NamedStorm(std::string, double, std::string, double);
NamedStorm(std::string);
NamedStorm();
// Destructor
//~NamedStorm();
// Get functions
int getStormCount();
double getStormPressure();
double getWindSpeed();
std::string getStormCategory();
std::string getName();
// Set functions
static void displayOutput();
static void sortByNames();
static void sortByWindSpeed();
static void getAverageWindSpeed();
static void getAverageStormPressure();
};
#endif // NAMEDSTORM_H_INCLUDED
Why is the definition of the default constructor the same as of the NamedStorm::NamedStorm(std::string) ? I would start from correcting that.
I have written a class as shown below:
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc()
{
cnt++;
}
int a;
public:
A(){ inc(); }
};
int main()
{
A d;
return 0;
}
I want to call the function inc through the constructor, but when i compile i am getting an error as:
/tmp/ccWR1moH.o: In function `A::inc()':
s.cpp:(.text._ZN1A3incEv[A::inc()]+0x6): undefined reference to `A::cnt'
s.cpp:(.text._ZN1A3incEv[A::inc()]+0xf): undefined reference to `A::cnt'
I am unable to understand what the error is... plz help...
Static field is not defined - Take a look at Why are classes with static data members getting linker errors?.
#include<iostream>
using namespace std;
class A
{
static int cnt;
static void inc(){
cnt++;
}
int a;
public:
A(){ inc(); }
};
int A::cnt; //<---- HERE
int main()
{
A d;
return 0;
}
Inside the class static int cnt; is only declared, and need to be defined. In C++ you usually declare in your .h .hpp files and then define your static class members in your .c and .cpp files.
In your case, you need to add
int A::cnt=0; // = 0 Would be better, otherwise you're accessing an uninitialized variable.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
C++: undefined reference to static class member
The following C++ code compiles well (using g++ -c) but it doesn't link giving the error: undefined reference toAbc::X'`
#include <iostream>
using namespace std;
class Abc {
public:
const static int X = 99;
};
int main()
{
Abc a1;
cout << &(Abc::X) << endl;
}
I want to know why this is not allowed?
You need to have that static member actually defined, not just declared...
Add this line before your main():
const int Abc::X = 99;
As of C++17 you can also do an inline static, in which case the above additional line of code in a .cpp file is not needed:
class Abc {
public:
inline const static int X = 99; // <-- "inline"
};
If the static member is used in a way which requires an lvalue (i.e. in a way that requires it to have an address) then it must have a definition. See the explanation at the GCC wiki, which includes references to the standard and how to fix it.
If you don't like to think about translation units, static initialization order and stuff like that, just change your static constants into methods.
#include <iostream>
using namespace std;
class Abc {
public:
inline static const int& X(){
static int x=99;
return x;
}
};
int main()
{
// Abc a1;
cout << &(Abc::X()) << endl;
}