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

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

Related

How to work with extern in different files

I have these files structure:
main.cpp
#include "main.h"
Map map;
Fruit fruit;
Stone stone;
main.h
extern Map map;
extern Fruit fruit;
extern Stone stone;
map.h
#include "main.h"
class Map {public: int size = 20;};
fruit.h
#include "main.h"
class Fruit { public: int pos = 1; draw() {return map.size;} };
stone.h
#include "main.h"
class Stone { public: draw() {return map.size * fruit.pos;} };
The problem is when I'm trying to use map.size and fruit.pos I get error:
'map': undeclared identifier
The same with stone. So, what's wrong?
main.h should include map.h not the other way around.
main.h should include fruit.h not the other way around.
main.h should include stone.h not the other way around.
Also you should add include guards to your header files.
EDIT
Here's one way that works, (I can't believe I recommending code like this but still)
// map.h
#ifndef MAP_H
#define MAP_H
class Map {public: int size = 20};
extern Map map;
#endif
// fruit.h
#ifndef FRUIT_H
#define FRUIT_H
#include "map.h"
class Fruit { public: int pos = 1; draw() {return map.size;} };
extern Fruit fruit;
#endif
// stone.h
#ifndef STONE_H
#define STONE_H
#include "map.h"
#include "fruit.h"
class Stone { public: draw() {return map.size * fruit.pos;} };
extern Stone stone;
#endif
// main.cpp
#include "map.h"
#include "fruit.h"
#include "stone.h"
Map map;
Fruit fruit;
Stone stone;
This is not how you are supposed to write code.
Files (*.h or *.cpp) should only include files that they directly depend upon.
Files should not include files that they do not depend upon.
One way to break cyclical dependencies is to put the implementation in the foo.cpp source file instead of inline in the foo.h header file.
One way to break dependencies on global variables is to instead pass them in as parameters instead of having them hard-coded into the routines.
Use of a forward declaration can be used to avoid including an header file that is only used to declare the type. Only when the details of the type, such as its methods and footprint, are not important. Alas, forward declarations for template classes are trickier.
For the files in the OP example, here's an alternative implementation incorporating those suggestions.
fruit.h
#ifndef FRUIT_H
#define FRUIT_H
class Map;
class Fruit {
public:
int pos = 1;
auto draw(Map const&) -> int;
};
#endif
map.h
#ifndef MAP_H
#define MAP_H
class Map {
public:
int size = 20;
};
#endif
stone.h
#ifndef STONE_H
#define STONE_H
class Fruit;
class Map;
class Stone {
public:
auto draw(Map const& map, Fruit const& fruit) -> int;
};
#endif
fruit.cpp
// Identity.
#include "fruit.h"
// Other dependencies.
#include "map.h"
auto Fruit::draw(Map const& map) -> int {
return map.size;
}
stone.cpp
// Identity.
#include "stone.h"
// Other dependencies.
#include "fruit.h"
#include "map.h"
auto Stone::draw(Map const& map, Fruit const& fruit) -> int {
return map.size * fruit.pos;
}
main.cpp
#include <iostream>
#include "fruit.h"
#include "map.h"
#include "stone.h"
using std::cout;
int main() {
auto map = Map{};
auto fruit = Fruit{};
auto stone = Stone{};
map.size = 17;
fruit.pos = 3;
cout << "map.size is " << map.size << "\n";
cout << "fruit.pos is " << fruit.pos << "\n";
cout << "fruit.draw(map) is " << fruit.draw(map) << "\n";
cout << "stone.draw(map, fruit) is " << stone.draw(map, fruit) << "\n";
}

Cant create object of type unorderedLinkedList

I have two .h files, the base class which is linkedListType and the derived class unorderedLinkedList. For some reason, it wont let me create an object from my unorderedLinkedList class. I'm also using templates throughout my program.
#include <iostream>
#include "unorderedLinkedList.h"
using namespace std;
int main()
{
unorderedLinkedList<int> list, subList;
int num;
cout << "Enter numbers ending with -999" << endl;
cin >> num;
}
#pragma once
#define UNORDEREDLINKEDLIST_H
#ifndef UNORDEREDLINKEDLIST_H
#include "linkedList.h"
#include <iostream>
using namespace std;
//creates class type unorderedLinkedList
template <class Type>
class unorderedLinkedList : public linkedListType<Type>
{
public:
bool search(const Type& searchItem) const;
//function to determine wether searchItem is in the list
void insertFirst(const Type& newItem);
};
#endif
#pragma once
#define LINKEDLISTTYPE_H
#ifndef LINKEDLISTTYPE_H
#include <iostream>
#include <cassert>
using namespace std;
template<class Type>
class linkedListType
{
public:
void divideMid(linkedListType<Type>& sublist);
// divide list
const linkedListType<Type>& operator=(const linkedListType<type>&);
// overload the assignment operator.
void initializeList();
};
#endif
In both of your header files, your #define and #ifndef statements are in the wrong order.
In each file, you are defining the guard value and then checking if it is defined, which it is, so the entire content of the header file is skipped.
You need to define the guard value only if it is not already defined:
#ifndef UNORDEREDLINKEDLIST_H
#define UNORDEREDLINKEDLIST_H
...
#endif
#ifndef LINKEDLISTTYPE_H
#define LINKEDLISTTYPE_H
...
#endif
As mentioned above you need to check if the include guard is defined before defining it otherwise it will never include in the first place.
Also you should use either #pragma once (if supported) or the #ifndef #define guards. It is not necessary to use both.
Again, to reiterate
-remove the #pragma once
-change the order of your ifndef / define
#ifndef UNORDEREDLINKEDLIST_H
#define UNORDEREDLINKEDLIST_H
...
#endif
#ifndef LINKEDLISTTYPE_H
#define LINKEDLISTTYPE_H
...
#endif

Define a static integer in a header with default value

How can I define an integer in a header file so that each cpp file which includes the header will have static const int id=0 while giving the ability to cpps to redefine it with other value.
I tried to used weak symbol but couldn't make it work.
If you are ok with preprocessor definitions you could do this:
// header.h
#ifndef CLASSID
#define CLASSID 0
#endif
static int id=CLASSID;
// class.cpp
#define CLASSID 1
#include "header.h"
This way a source file may override the default, but may also omit it, which is the sort of weak approach you mentioned.
Here's another solution that uses static variables:
// log.h
#ifndef LOG_H
#define LOG_H
#include <iostream>
#define SETLOGID(v) static logidsetter _logidsetter(_logid, v);
#define LOG(v) std::cout << "id: " << _logid << ": " << (v) << std::endl;
class logidsetter
{
public:
logidsetter(int &id, int val)
{
id = val;
}
};
static int _logid = 0;
#endif
// myclass.h
class myclass
{
public:
myclass();
void run(void);
};
// myclass.cpp
#include "log.h"
#include "myclass.h"
SETLOGID(42)
myclass::myclass()
{
LOG("myclass::cons");
}
void myclass::run(void)
{
LOG("myclass::run");
}
// main.cpp
#include "myclass.h"
#include "log.h"
SETLOGID(1)
int main()
{
myclass mc;
LOG("here's main");
mc.run();
}
The log header defines the static int _logid and provides the macro SETLOGID and the class idsetter. The cpp file may use SETLOGID to redefine the static value. This is done with an instantiation of the class idsetter along with the address of _logid and the desired value. The trick allows to bypass C++'s One Definition Rule.
The output looks like:
id: 42: myclass::cons
id: 1: here's main
id: 42: myclass::run

cannot instantiate abstract class but class isn't abstract

I've looked this up and the closest thing I found was this except I don't have any forward declarations. I only have one pure virtual function in the base class which I'm implementing in the subclass as follows:
Command.h
#ifndef _COMMAND_H_
#define _COMMAND_H_
#include <string>
#include "Stack.h"
#include "Number.h"
class Command
{
public:
std::string cmdType;
Command(void);
Command (std::string cmdType);
virtual void executeCommand(Stack<Number> & stack) = 0;
~Command (void);
};
#endif // !defined _COMMAND_H_
Command.cpp
Command::Command(void)
:cmdType("")
{}
Command::Command(std::string cmdType)
:cmdType(cmdType)
{}
Command::~Command(void)
{}
Number.h
#ifndef _NUMBER_H_
#define _NUMBER_H_
#include "Command.h"
#include "Stack.h"
class Number : public Command
{
public:
Number (float num);
void executeCommand(Stack<Number> & stack);
float val;
~Number (void);
};
#endif // !defined _NUMBER_H_
Number.cpp
#include "Number.h"
Number::Number(float num)
:val(num)
{
cmdType = "hi";
}
void Number::executeCommand(Stack<Number> & stack)
{
stack.push((*this));
}
File error occurs:
Error 4 error C2259: 'Number' : cannot instantiate abstract class c:\...\add.cpp 34
Add.cpp
#include "Add.h"
Add::Add(void)
:Binary("+")
{
}
Add::~Add(void)
{
}
void Add::executeCommand(Stack<Number> & numStack)
{
Number num1 = numStack.top(); //THIS LINE HAS THE ERROR
numStack.pop();
Number num2 = numStack.top();
numStack.pop();
float tempVal = num2.val + num1.val;
num1.val = tempVal;
numStack.push(num1);
}
Add.h
#ifndef _ADD_H_
#define _ADD_H_
#include "Stack.h"
#include "Number.h"
#include "Binary.h"
class Add : public Binary
{
public:
Add (void);
void executeCommand (Stack<Number> & numStack);
~Add (void);
};
#endif // !defined _ADD_H_
This is a circular dependency problem.
Command.h includes Number.h
Number.h includes Command.h
Usually it is solved by replacing one of the includes with a forward declaration, try forward-declaring Number in Command.h instead of including Number.h; move that include to Command.cpp.

The infamous 'function' was not declared in this scope

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.