The infamous 'function' was not declared in this scope - c++

I know this has been asked many times, but I can not understand this problem. This my header file:
#ifndef TASK_H
#define TASK_H
#include "storage_adaptors.hpp"
#include <boost/numeric/ublas/vector.hpp>
class Task {
private:
boost::numeric::ublas::vector<double> taskPosistionConstraint;
boost::numeric::ublas::vector<double> initialPosition;
boost::numeric::ublas::vector<double> finalPosition;
double pathLength;
int taskType;
public:
Task();
Task(double* _initialPoint, double* _finalPoint, int type);
double getLength();
int getTaskType();
~Task();
};
#endif /* TASK_H */
and this is the cpp file:
#include "Task.h"
const int TASK_SIZE = 3;
Task::Task() {
}
Task::~Task() {
}
Task::Task(double* _initialPoint, double* _finalPoint, int type) {
finalPosition = make_vector_from_pointer(TASK_SIZE,_finalPoint);
initialPosition = make_vector_from_pointer(TASK_SIZE, _initialPoint);
}
The error occurs at make_vector_from_pointer function that is defined in the storage_adaptors.hpp which is included in the Task.h which is a boost hpp file.
If the header is added to the class header file, why I'm having out of scope error:
Task.cpp:21: error: `make_vector_from_pointer' was not declared in
this scope

If it's a boost function, shouldn't it be boost::make_vector_from_pointer' ? Or whatever namespace it's in if not directly in the boost namespace.

Related

C++ Class inheritance in different files

I'm trying to learn Inheritance mechanism in C++, I have made a Bancnote(Bills) class, and I want to make a class Card inheriting all the functions and variables from Class Bancnote.
And I get this type of error :
include\Card.h|6|error: expected class-name before '{' token|
BANCNOTE.H
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include <iostream>
#include "Card.h"
using namespace std;
class Bancnote
{
public:
Bancnote();
Bancnote(string, int ,int ,int );
~Bancnote( );
int getsumacash( );
void setsumacash( int );
int getsumaplata( );
void setsumaplata( int );
int getrest( );
void setrest( int );
string getnume( );
void setnume( string );
void ToString();
protected:
private:
string nume;
int sumacash;
int rest;
static int sumaplata;
};
#endif // BANCNOTE_H
BANCNOTE.CPP
#include <iostream>
#include "Bancnote.h"
#include "Card.h"
using namespace std;
int Bancnote::sumaplata=0;
Bancnote::Bancnote(string _nume,int _sumacash,int _rest, int _sumaplata )
{
this->nume=_nume;
this->sumacash=_sumacash;
this->rest=_rest;
this->sumaplata=_sumaplata;
}
Bancnote::Bancnote()
{
this->nume="";
this->sumacash=0;
this->rest=0;
this->sumaplata=0;
}
Bancnote::~Bancnote()
{
cout<<"Obiectul"<<"->" <<this->nume<<"<-"<<"a fost sters cu succes";
}
string Bancnote::getnume()
{
return nume;
}
void Bancnote::setnume(string _nume)
{
this->nume=_nume;
}
int Bancnote::getsumacash()
{
return sumacash;
}
void Bancnote::setsumacash(int _sumacash)
{
this->sumacash=_sumacash;
}
int Bancnote::getsumaplata()
{
return sumaplata;
}
void Bancnote::setsumaplata(int _sumaplata)
{
this->sumaplata=_sumaplata;
}
int Bancnote::getrest()
{
return rest;
}
void Bancnote::setrest(int _rest)
{
this->rest=_rest;
}
void Bancnote::ToString()
{
cout<< "-----"<<getnume()<< "-----"<<endl;
cout<<"Suma Cash: "<<this->getsumacash()<<endl;
cout<<"Suma spre plata: "<<this->getsumaplata()<<endl;
cout<<"Restul:"<<this->getrest()<<endl;
}
CARD.H
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
class Card: public Bancnote
{
public:
Card();
virtual ~Card();
protected:
private:
};
#endif // CARD_H
You have messed up the includes. What you have is more or less this:
Bancnote.h:
#ifndef BANCNOTE_H
#define BANCNOTE_H
#include "Card.h" // remove this
struct Bancnote {};
#endif
Card.h
#ifndef CARD_H
#define CARD_H
#include "Bancnote.h"
struct Card : Bancnote {}; // Bancnote is not yet declared
// when compiler reaches here
#endif
When in main you include Bancnote.h then this header includes Card.h so you try to declare Card before Bancnote is declared. Actually Bancnote does not need the definition of Card, so simply removing the include should fix it.
PS: there are other issues (see comments below your question). Most importantly it is not clear why a Card is a Bancnote. Second, never put a using namespace std; inside a header! (see here why)

Defining member variable in header without class definition

I've got two files, list.cpp and Header.h. Segments of the files are below. I know that if the header file is for a class, it is setup different. E.g.
class MyClass
{
public:
void foo();
int bar;
};
However, since I'm not really working with a class here (correct me if I'm wrong), am I not able to declare things under public: and private like below?
Also, if I were to place the global variable rescan in the header file as a member variable, below the function definitions, only the main function can see the variable. Why is it not within the scope of the other functions?
list.cpp:
#include <boost/algorithm/string.hpp>
#include <vector>
using namespace std;
vector<int> results;
bool rescan;
int main()
{
vector<vector<string>> list;
int success = readFile(list);
vector<vector<string>> bad = findMe(list);
system("pause");
return 0;
}
vector<vector<string>> findMe(vector<vector<string>> find)
{
rescan = true;
}
Header.h:
#pragma once
#ifndef HEADER_H_INCLUDED
#define HEADER_H_INCLUDED
#include <string>
#include <vector>
std::vector<std::vector<std::string>> findMe(std::vector<std::vector<std::string>>);
#endif
EDIT: I tried this in my header file:
public:
bool rescan;
But I got "syntax error: 'public'
If you want your global to be visible in other translation units (TU) (other files), you have to declare them extern in those other TUs:
Header.h:
// Include guard omitted
extern bool rescan; // Declaration
file.cpp
#include "Header.h"
bool rescan = false; // Definition
// ...
file2.cpp
#include "Header.h" // To see extern bool rescan;
void foo()
{
rescan = true;
}
// ...

c++ error: mysterious conflicting declaration error

I have carefully looked into my code but don't see why this error comes out.
The error message is the following:
main.cc: In function ‘int main()’:
main.cc:12: error: conflicting declaration ‘traj dim’
main.cc:11: error: ‘dim’ has a previous declaration as ‘unsigned int dim’
and one can reproduce it with the following command
g++ -o a.out realvector.cc traj.cc main.cc
My main.cc is
#include "realvector.h"
#include "traj.h"
using namespace std;
int main() {
unsigned int dim=1000;
traj TRAJ(dim);
return 1;
}
traj is defined in traj.h as
#ifndef TRAJ
#define TRAJ
#include "realvector.h"
class traj{
public:
traj(unsigned int);
~traj();
void next(double &);
private:
unsigned int it,nt; // index, total array size
double dt; // step time
RealVector r,v,a;
};
#endif
the constructor is defined in traj.cc
#include "realvector.h"
#include "traj.h"
traj::traj(unsigned int dim) : nt(dim) {
RealVector r(nt),v(nt),a(nt);
it=0;
}
traj::~traj(){
r.~RealVector();
}
Any idea why this error comes out? Also, is the way to define r,v,a correct? RealVector is a home-defined class with its constructors defined as the following
#include "realvector.h"
using namespace std;
RealVector::RealVector() {}
RealVector::RealVector(unsigned int n)
: dim(n) {
data = new double[dim];
for (int i=0; i<dim; i++)
data[i]=0;
}
RealVector::~RealVector(){
delete[] data;
}
with realvector.h as
#ifndef REAL_VECTOR_H
#define REAL_VECTOR_H
#include <iostream>
class RealVector {
public:
RealVector();
RealVector(unsigned int n);
~RealVector();
int dim;
double* data;
};
#endif
The code is not complete... as a wild guess you also have a TRAJ macro that makes reading what the code really is impossible.
In traj.h you have
#define TRAJ
which defines TRAJ as an empty "string" and this leads to this replace by the preprocessor:
traj TRAJ(dim);
to
traj (dim);
which produces the error message.
I guess you should rename TRAJ in the include file to TRAJ_H and then it works.

C++ error: incomplete type used in nested name specifier

I have the following header helper.h:
#ifndef ADD_H
#define ADD_H
class Helper{
public:
static float calculateSpriteSize(float imgSize, float screenSize);
};
#endif
This is my helper.cpp:
#include "block.h"
#include "helper.h"
float Helper::calculateSpriteSize(float imgSize, float screenSize)
{
return ((imgSize/screenSize)*100);
}
But, for some reason, when I call my function calculateSpriteSize on my running code by doing:
#include "header.h"
int main(void){
float h = Helper::calculateSpriteSize( 168.0f, 170.0f );
)
I get the following error:
error: incomplete type 'Helper' used in nested name specifier
Any help would be appreciated.
Block.h looks as follows:
#ifndef ADD_H
#define ADD_H
class Block{
private:
int imgID;
int life;
float price;
public:
Block();
void setImgID(int imgID);
int getImgID();
};
#endif
And block.cpp looks as follows:
#include "block.h"
Block::Block()
{
}
void Block::setImgID(int imgID)
{
this->imgID = imgID;
}
int Block::getImgID()
{
return imgID;
}
UPDATE: I added Helper to the class definition as suggested by Rakete1111. This did not fix the issue though.
UPDATE 2: Changed forward declaration to include. Added other include that was in my code in case its important.
The type introduced by forward declaration is incomplete type. But member function invoking requires the type to be complete, otherwise how does the compiler know whether the member exists or not, and its signature?
You need to include the header file.
#include "helper.h"
int main(void){
float h = Helper::calculateSpriteSize( 168.0f, 170.0f );
)
EDIT
You're using the same macro ADD_H in both "block.h" and "helper.h". It means for
#include "block.h"
#include "helper.h"
the second including would fail, the content of helper.h won't be included at all.
Change the including guard macro name to be unique, better to make it conform to the name of file name. Such as HELPER_H and BLOCK_H.

error: ‘whatever class' was not declared in this scope

main contains:
#include "num.h"
num * intObj = new num;
num.h contains:
#ifndef __EXPR_H__
#define __EXPR_H__
#include <string>
class num : public Expr {
//
};
#endif
expr.h contains:
#ifndef __EXPR_H__
#define __EXPR_H__
#include <string>
class Expr {
public:
virtual int eval() const = 0;
virtual std::string prettyPrint() const = 0;
virtual ~Expr();
};
#endif
Then I get:
error: ‘num’ was not declared in this scope
num * intObj = new num;
^
What can be the reason for this? I have also declared the class Expr in a different .h file, which is also included in main.
I get the same error with all the new classes I declared and am using.
You are using the same header guard __EXPR_H__ for both headers. Only one will be defined.
Change __EXPR_H__ in num.h to __NUM_H__ and it will be fine.
Try one of the following:
#include "expr.h" /* before num.h */
#include "num.h"
num * intObj = new num;
or
#ifndef __NUM_H__ /* Header file guard for num.h not expr.h here */
#define __NUM_H__
#include <string>
include "expr.h" /* #ifndef __EXPR_H and #define __EXPR_H__ in this .h file */
class num : public Expr {
//
};
#endif