error undefined reference to .. 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 9 years ago.
so I have a class x that is being used by class y and it's also going to be used by other classes.
.h for class x
#pragma once
#include <string>
#ifndef X_H
#define X_H
class x
{
public:
x();
const std::string & getName() const;
int getQuantity();
private:
std::string name;
int quantity;
};
#endif
.cpp for x
#include <string>
#include "x.h"
using namespace std;
x::x()
: name(),quantity(0)
{
}
const string & x::getName() const
{
return name;
}
const string & x::getQuantity() const
{
return quantity;
}
this is the .h for class y
#pragma once
#include <string>
#include <array>
#include "x.h"
class y
{
public:
static const size_t number = 20;
y();
float getTotal();
private:
std::array<X*, number> arrayList;
};
and this is the .cpp for class y
#include "y.h"
#include "x.h"
#include <array>
#include <string>
#include <iostream>
using namespace std;
y::y()
: arrayList()
{
}
float y::getTotal()
{
float total=0.0;
for(int i=0; i< number; i++)
{
if(arrayList[i] != nullptr)
{
total += arrayList[i]->getQuantity();
}
}
}
methods in the y class uses an array of pointers to method y and I'm trying to use some methods from class x using the array members but I get an error saying:
undefined reference to `x::x(...)
I think it has something to do with the preprocessors or the headers.

This means that you forgot to define the default constructor X::X() or some other constructor with parameters ( what does x::x(...) mean?) of class X. You only declared it in the class definition.
Or the other reason is that the module with the constructor definition was not included in the project build.

In class x you have explicitly declared the default constructor x() but you have not defined it. If you want to use the default constructor, remove its definition or define it with x::x():name(std::string()),quantity(0){}

Related

Why my random function always not undeclared?

I'm writing my code on linux . But g++ always tells me"Use of undeclared identifier 'random'".I don't know why I have declare it in "Myvector.h"
my code is like :
Myvector.h
class MyVector {
private:
std::vector<double> data;
const int N;
static bool _bDim;
public:
MyVector(); //默认初始化
MyVector(int a); //设置维度初始化
MyVector(std::initializer_list<double> list);
~MyVector();
double &operator[](int);
MyVector &operator=(const MyVector a) {
MyVector b(outN(a));
this->data = a.data;
return *this;
};
friend MyVector random(int a);
}
#endif // MYVECTOR_H_
Myvector.cpp
#include "Myvector.h"
#include <cstdlib>
#include <iostream>
#include <math.h>
#include <time.h>
using namespace std;
bool MyVector::_bDim = true;
MyVector::MyVector() : N(3) {
data = vector<double>(N, 0.0);
_bDim = false;
};
MyVector::MyVector(int a) : N(a) {
data = vector<double>(N, 0.0);
_bDim = false;
};
MyVector::MyVector(std::initializer_list<double> list) : N(list.size()) {
for (auto i = list.begin(); i != list.end(); i++) {
data.push_back(*i);
}
};
MyVector::~MyVector(){
};
double &MyVector::operator[](int i) { return data[i]; }
MyVector random(int a){
MyVector u(a);
srand(time(NULL));
for(int i=0;i<a;i++){
u[i]=rand();
}
return u;
}
main.cpp
#include "Myvector.h"
#include <iostream>
#include<cstdlib>
#include<math.h>
#include <time.h>
using namespace std;
int main(){
MyVector z=random(1);
return 0;}
In fact ,I just know nothing about it. Is there someone going to help me?Thank you.
Below is nothing meaningful. I just need more words to ask this problem.
In the main function of the main.cpp file the following function is called:
MyVector z=random(1);
This appears to be a function which takes a single int argument. Additionally, there is such a function defined in the Myvector.cpp but not declared in Myvector.h (i.e., the main.cpp file does not see any function declaration for the definition).
Update the Myvector.h header to declare the MyVector random(int a) function. Also, the friend declaration is for a random function with 2 parameters, which doesn't look right.
You have to declare the function random somewhere, e.g.
#include "Myvector.h"
#include <iostream>
#include<cstdlib>
#include<math.h>
#include <time.h>
using namespace std;
MyVector random(int);
int main(){
MyVector z=random(1);
return 0;
}
The problem is random in your main function. That one is not declared.
Declaring a friend function means that function has access to the class as if it were a method. It doesn't declare the function at any time, just allows it inside the class.
Your random function is defined is some header file you have included. In your error message you see the return type of random is long int. And you have declared it as MyVector. I am not sure if math.h or time.h have it.
Solution 1: Change the name of your function.
Solution 2: Put your function in a namespace in order to avoid name ambiguity.

Error: No matching function for call to constructor [duplicate]

This question already has answers here:
Initialize base class with no default constructor in constructor of derived class
(3 answers)
Closed 3 years ago.
I'm learning oop in c++ and have the following error:
In constructor 'pflFr1::pflFr1()'
Error: No matching function for call to worldBuilder::worldBuilder()
The class worldbuilder is generating protected 2d arrays. The class pflfr1 is inherting from worldbuilder. But somehow, the constructor of worldbuilder has a problem.
My code:
main.cpp:
#include <iostream>
#include "pflfr1.h"
#include "worldbuilder.h"
#include <vector>
using namespace std;
int main()
{
srand(time(0));
int sl = 10;
worldBuilder w_obj(sl);
w_obj.buildPhyWelt();
w_obj.buildVegWelt();
pflFr1 objekt();
return 0;
}
worldbuilder.h:
#ifndef WORLDBUILDER_H
#define WORLDBUILDER_H
#include <vector>
using namespace std;
class worldBuilder
{
public:
worldBuilder(int sl_);
vector<vector<int> > buildPhyWelt();
vector<vector<int> > buildVegWelt();
protected:
vector<vector<int> > phyWelt;
vector<vector<int> > vegWelt;
int sl;
private:
};
#endif // WORLDBUILDER_H
worldbuilder.cpp:
#include <iostream>
#include "worldbuilder.h"
#include <vector>
using namespace std;
worldBuilder::worldBuilder(int sl_)
: sl(sl_)
{
}
vector<vector<int> > worldBuilder::buildPhyWelt()
{
phyWelt.resize(sl, vector<int>(sl));
// initialisiere physische Welt
// 1 = land, -1 ist meer
for(int i=0; i<sl; i++)
{
for(int j=0; j<sl; j++)
{
phyWelt[i][j] = 1;
}
}
}
vector<vector<int> > worldBuilder::buildVegWelt()
{
vegWelt.resize(sl, vector<int>(sl));
// initialisiere Nahrung
for(int i=0; i<sl; i++)
{
for(int j=0; j<sl; j++)
{
if(rand()%100<=2)
{
vegWelt[i][j] = 1;
}
else
{
vegWelt[i][j] = 0;
}
}
}
}
pflfr1.h:
#ifndef PFLFR1_H
#define PFLFR1_H
#include <vector>
#include "worldbuilder.h"
using namespace std;
class pflFr1: protected worldBuilder
{
public:
pflFr1();
protected:
private:
int y;
int x;
int hp;
};
#endif // PFLFR1_H
pflfr1.cpp:
#include <iostream>
#include <pflfr1.h>
#include <cstdlib>
#include <ctime>
using namespace std;
pflFr1::pflFr1()
: hp(10)
{
int initPosY = rand()%sl;
int initPosX = rand()%sl;
y = initPosY;
x = initPosX;
}
Your default constructor pflFr1::pflFr1() 's implementation is rewritten as
pflFr1::pflFr1()
: worldBuilder(), hp(10){
...
}
Because the pflFr1 class derives from worldBuilder, its constructor must be called as part of pflFr1 object creation. The default behaviour is to call the default constructor of the base - worldBuilder().
But this constructor does not exist. You only declared worldBuilder::worldBuilder(int sl_);. By doing so you disabled the automatic generation of default constructors for classes, which is done only if no user-defined constructor was declared.
Either you can write worldBuilder()=default; inside the class, which will leave s1 uninitialized. Or you can use delegating constructors - worldBuilder():worldBuilder(0){}.

How to import a class in another C++ 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 6 years ago.
I meet a problem when constructing a class. class "Graph" import a class "Bag" in another file and use "Bag" as its component.
//Graph.h
#ifndef GRAPH_H
#define GRAPH_H
#include <fstream>
#include <iostream>
#include <vector>
#include "Bag.h"
class Bag;
class Graph
{
public:
Graph(int V);
Graph(std::ifstream& in_file);
int getV() { return V; }
int getE() { return E; }
void addEdge(int v, int w);
void showadj() ;
private:
int V;
int E;
std::vector<Bag> adj;
};
#endif
And "Bag.h" is as follow:
//Bag.h
#ifndef BAG_H
#define BAG_H
#include <vector>
#include <iostream>
class Bag
{
public:
Bag();
void addBag(int i) { content.push_back(i); }
void showBag();
private:
std::vector<int> content;
};
#endif
Graph.cpp:
//Graph.cpp
#include "Graph.h"
#include "Bag.h"
Graph::Graph(int V) : V(V), E(0)
{
for (int i = 0; i < V; i++)
{
Bag bag;
adj.push_back(bag);
}
}
Bag.cpp(sorry, forget it):
#include "Bag.h"
void Bag::showBag()
{
for (int i : content)
{
std::cout << i << " ";
}
}
When I try to complie these two classes, an error comes out saying:
C:\Users\ADMINI~1\AppData\Local\Temp\ccMj4Ybn.o:newtest.cpp:(.text+0x1a2): undef
ined reference to `Bag::Bag()'
collect2.exe: error: ld returned 1 exit status
You need to also implement your constructor Bag::Bag() as it is missing in your Bag.cpp file.
This is what the error tells you. If you don't need the constructor, then you should remove it from your class definition, which would also solve this error.
An alternative would be to provide an empty constructor in your Bag.h file
class Bag
{
public:
Bag() {}
...
}

Data "member not declared in this scope"

I'm trying to create a vector which will store objects. I have added to the header file of the class as a private data member.
I am trying to initialize this vector as being empty (so that I can add objects to it later on in the program) but when I compile this program to test, this error is returned:
...error: '_bookingVector' was not declared in this scope|
I think the problem is with my initialization list on my default constructor(_bookingVector is obviously the vector):
Schedule::Schedule() : _bookingVector()
{ }
Is my syntax wrong? Or are vectors initialized differently?
Here is my code:
Schedule.h
#ifndef SCHEDULE_H
#define SCHEDULE_H
#include "Booking.h"
#include <vector>
using namespace std;
class Schedule
{
public:
Schedule();
void AddBooking(int bday, int btime, int btrainer, int bid);
void RemoveBooking(int bday, int btime);
void DisplaySchedule();
void DisplayAvailableTimeSlots();
//For Testing
void DisplayDebug();
private:
vector<Booking> _bookingVector;
};
#endif // SCHEDULE_H
Schedule.cpp
#include "Schedule.h"
#include "Booking.h"
#include <vector>
#include <iostream>
Schedule::Schedule() : _bookingVector()
{ }
void AddBooking(int bday, int btime, int btrainer, int bid){
Booking bookingObject(bday, btime, btrainer, bid);
_bookingVector.push_back(bookingObject);
}
void DisplayDebug(){
for(int i = 0; i < _bookingVector.size(); ++i){
cout << _bookingVecotr[i] << endl;
}
}
I'm very eager to learn what I'm doing wrong and fix it.
The issue is not with the constructor, which looks fine if unnecessary1. The issue is that you have defined AddBooking and DisplayDebug as non-member functions, but these should be members in order to access other members of the class.
Modify the definitions to be in the scope of the Schedule class thus:
void Schedule::AddBooking(int bday, int btime, int btrainer, int bid) { ...
^^^^^^^^^^
void Schedule::DisplayDebug(){ ...
^^^^^^^^^^
Also, don't say using namespace std in a header file (I'd go further and say don't say it anywhere but there isn't universal agreement on that.)
1 Your default constructor does not do anything that the compiler-generated one wouldn't do. You can safely remove it.

"undefined reference to" while i am trying to inherit vector class [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 9 years ago.
Main.cpp
#include <iostream>
#include "include/Numbers.h"
#include <vector>
#include <string>
#include <fstream>
using namespace std;
int main()
{
ofstream myofile;
ifstream myifile;
myofile.open("output.txt");
myifile.open("input.txt");
int number;
Numbers input;
if(myifile.is_open())
while(myifile >> number) {
input.push_back(number);
}
cout << input.size() << endl;
myofile.close();
myifile.close();
cout << "Hello world!" << endl;
return 0;
}
Numbers.h
#ifndef NUMBERS_H
#define NUMBERS_H
#include <vector>
class Numbers: public std::vector<int>
{
public:
Numbers();
~Numbers();
int size();
Numbers prob();
protected:
private:
};
#endif // NUMBERS_H
Numbers.cpp
#include "../include/Numbers.h"
#include <iostream>
using namespace std;
Numbers::Numbers()
{
}
Numbers::~Numbers()
{
//dtor
}
I am trying to create a new Numbers class which inherits functions from vector class.
The error I am getting is undefined reference to 'Numbers::size()' although the push_back function didn't give any problem
I am using codeblocks to write my code, and I have included all files in the build properties
First, what you are doing there is not a good idea. It is generally not intended to use STL-classes as base-classes (except for those especially designed for this, such as std::unary_function). std::vector does not have any virtual methods, so it does not have any value as a public base-class. Making things worse, std::vector does not even have a virtual destructor, so when you use it polymorphically, you will probably create a memory leak:
void deleteVector(std::vector<int>* v)
{
delete v;
}
deleteVector( new Numbers() );
In your case, you declared a Method Numbers::size which is not defined in the source-file. You could use a using declaration to use the size-method of your base-class, but that is not needed for public methods.
class Numbers: private std::vector<int>
{
public:
using std::vector<int>::size;
};