1
  • You could have forgot to include problemclass.h from the file where you are using ProblemClass.
  • You could have misspelled the name of ProblemClass either in its own header file or in the place where you are using it. This can be
    hard to spot if it is a capitalization error such as writing Problemclass or problemClass instead of ProblemClass.
  • You could have copy-pasted your inclusion guard #defines from one header file to another and then forgot to change the defined names. Then only the first of those two included header files would take effect.
  • You could have placed ProblemClass in a namespace A, in which case you must refer to ProblemClass as A::ProblemClass if you are referring to it from outside the namespace A.
  • You may be using templates and not expecting two-phase lookup to work the way it does.
  • You could have misspelled the file name in your include. The compiler would not report an error on that if you also have an old version of that file under the misspelled name.
  • You could have made ProblemClass a macro that only gets defined after you include problemclass.h, in which case what you see as ProblemClass gets replaced by something else by the macro preprocessor.
  • You could have defined ProblemClass in a header file other than problemclass.h and then problemclass.h actually defines something else.

The above was taken from another similar question, I found the points useful but none actually solved my problem, stated hereunder:

I'm creating a natural language processor for a robot, giving the software various objects to represent real world items in its environment (Blocks world for now), one of the objects defined in Block:

    /*
 * Block.h
 *
 *  Created on: 11 Mar 2015
 *      Author: Edward
 */

#ifndef BLOCK_HPP_
#define BLOCK_HPP_

#include "typedefinitions.h"
#include "dynamixel.h"
#include "BasicRobotFunctions.hpp"

class Block {
public:
    bool grounded;
    //TODO position is a deprecated variable, replace with distance from
    //pos position;
    int number;
    int distance;
    int object_brightness;
    Block(BasicRobotFunctions basic);
    virtual ~Block();
    void setBrightness(int brightness);
    void setGrounded(bool ground);
    //void setPosition(int x, int y);
    void setDistance(int number);
    void setNumber(int number);
    //pos getPosition();
    int getNumber();
    int getDistance();
    bool getGrounded();
    int getBrightness();
    int lookAround(BasicRobotFunctions basic);
};

#endif /* BLOCK_H_ */

with source file:

/*
 * Block.cpp
 *
 *  Created on: 11 Mar 2015
 *      Author: Edward
 */

#include "Block.hpp"

#define DEFAULT_PORTNUM 3   // COM3
#define DEFAULT_BAUDNUM 1  //  1Mbps

Block::Block(BasicRobotFunctions basic) {
    grounded = false;
    number = Block::lookAround(basic);
}

Block::~Block() {}

void Block::setGrounded(bool ground){
    grounded = ground;
}
/*
void Block::setPosition(int x, int y){
    position.x = x;
    position.y = y;
}*/

void Block::setDistance(int dist){
    distance = dist;
}

void Block::setNumber(int num){
    number = num;
}

bool Block::getGrounded(){
    return grounded;
}
/*
pos Block::getPosition(){
    return position;
}*/

int Block::getNumber(){
    return number;
}

int Block::getDistance(){
    return distance;
}

int Block::getBrightness(){
    return object_brightness;
}

//TODO Arrange function to incorporate Turn() in BasicRobotFunctions
int Block::lookAround(BasicRobotFunctions basic){
    int num = 0;
    dxl_initialize(DEFAULT_PORTNUM,DEFAULT_BAUDNUM);
    for(int i = 0;i<360;i++){
        dxl_write_word(11,32,-255);
        dxl_write_word(11,30,200);
        basic.Step(1);
        dxl_write_word(11,32,255);
        dxl_write_word(11,30,100);
        if(dxl_read_byte(100,32) >= dxl_read_byte(100,52)){
            num++;
        }
    }
    dxl_terminate();
    return num;
}

void Block::setBrightness(int bright){
    object_brightness = bright;
}

I am however receiving the following compilation error from the constructor and from the turnAround(BasicRobotFunctions) method:

In file included from Robot.hpp:11,
                 from BasicRobotFunctions.hpp:12,
                 from main.cpp:8:
Block.hpp:23: error: expected `)' before 'basic'
Block.hpp:35: error: 'BasicRobotFunctions' has not been declared
make.exe: *** [main.o] Error 1

Having checked my other classes utilizing objects as variables I get the same error. In response to the points in the quote: - BasicRobotFunctions.hpp is included - the class name is spelt the same in all different instances mentioning it - I didn't copy paste any inclusion guard - I didn't use any namespaces in the project - Nor am I using any templates - the file name isn't misspelled in my include - I haven't defined any macros in the program - I made sure every class was defined in its own header file

Is there any other issue my system could possibly have, any mistake I'm making or simply anything I'm doing which is bad programming practice here?

  • 1
    Please show your BasicRobotFunctions.hpp. The problem is likely in there (possible include recursion). – The Dark Apr 07 '15 at 22:21
  • You have circular includes (confirmed by the error snippet). Looking for the right duplicate. – Ben Voigt Apr 07 '15 at 22:21
  • You already found the question with the right answer, however you failed to read past the first answer. Look at [the second answer](http://stackoverflow.com/a/16628994/103167) – Ben Voigt Apr 07 '15 at 22:23
  • See also http://stackoverflow.com/a/5642303/103167 and http://stackoverflow.com/a/7714388/103167 and http://stackoverflow.com/questions/625799/resolve-circular-dependencies-in-c – Ben Voigt Apr 07 '15 at 22:25

2 Answers2

1

The cause of your problem:

You have a header file circular dependency problem.

main.cpp includes BasicRobotFunctions.hpp
which includes Robot.hpp
which includes Block.hpp
which includes BasicRobotFunctions.hpp.

If your header files are properly guarded against multiple inclusion (which it seems that they are), Block.hpp won't see the definitions of BasicRobotFunctions.hpp because it is already in the middle of including it.


How to spot the problem:

The source of this problem is apparent in the compilation error message and in your Block.hpp file.

The compiler is reporting an error in Block.hpp, and it is describing line by line how it got to that file via inclusions. The source to your Block.hpp file makes it clear that it is trying to include BasicRobotFunctions.hpp.


The fix:

In your case, you can modify your method signatures in Block.hpp to use a (perhaps constant) reference to the BasicRobotFunctions type, and then forward declare the type. This allows you to eliminate the dependency on the BasicRobotFunctions.hpp header file. (Block.cpp would likely need to include both Block.hpp and BasicRobotFunctions.hpp.)

//...
#include "typedefinitions.h"
#include "dynamixel.h"

class BasicRobotFunctions; // Forward declaration

    //...
    Block(const BasicRobotFunctions &basic);
    //...
    int lookAround(const BasicRobotFunctions &basic);
    //...

You may be able to avoid this problem in the future by minimizing what headers are required to allow your header file to compile. This means your header file should:

  • Use forward declarations to types that are used.
  • Use references to forward declared types.

You can check that your header file has minimized its dependencies by making sure it compiles by itself. I accomplish this by including the header file first in a corresponding source file, and then make sure the source file compiles.

// Foo.cpp
#include "Foo.hpp"
//...
jxh
  • 69,070
  • 8
  • 110
  • 193
  • Thanks! I fixed the error by moving the function which was calling BasicRobotFunctions from Block into BasicRobotFunctions :) – Edward Bamber Apr 08 '15 at 18:47
0

Well you can put a forward declaration before the class as

class BasicRobotFunctions; class Block { public: bool grounded; //TODO position is a ...

but this kind of error means that the #include "BasicRobotFunctions.hpp" don't declare the BasicRobotFunctions. It's possible a trouble with code guards? The circular inclusion can be solved using the forward declaration, putting correct guards in headers and moving some includes to source files.