C++ make error: object_var has not been declared
C++ make error: object_var has not been declared
- 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.
以上内容摘自另一个类似问题,我发现这些点很有用,但 none 确实解决了我的问题,声明如下:
我正在为机器人创建一个自然语言处理器,为软件提供各种对象来表示其环境中的真实世界项目(目前为 Blocks 世界),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_ */
源文件:
/*
* 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;
}
然而,我从构造函数和 turnAround(BasicRobotFunctions) 方法收到以下编译错误:
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
检查了我的其他 classes 使用对象作为变量我得到了同样的错误。
针对引用中的要点:
- BasicRobotFunctions.hpp 包括在内
- class 名称在所有提及它的不同实例中拼写相同
- 我没有复制粘贴任何包含保护
- 我没有在项目中使用任何命名空间
- 我也没有使用任何模板
- 文件名在我的包含中没有拼错
- 我没有在程序中定义任何宏
- 我确保每个 class 都在其自己的头文件
中定义
我的系统是否可能存在任何其他问题、我犯的任何错误或我正在做的任何错误的编程实践?
您的问题原因:
你有一个头文件循环依赖问题。
main.cpp
包括 BasicRobotFunctions.hpp
其中包括 Robot.hpp
其中包括 Block.hpp
其中包括 BasicRobotFunctions.hpp
.
如果您的头文件被适当地防止多重包含(看起来确实如此),Block.hpp
将看不到 BasicRobotFunctions.hpp
的定义,因为它已经在包含的中间它。
如何发现问题:
此问题的根源在编译错误消息和您的 Block.hpp
文件中很明显。
编译器在 Block.hpp
中报告错误,它逐行描述它是如何通过包含到达该文件的。 Block.hpp
文件的来源清楚地表明它正在尝试包含 BasicRobotFunctions.hpp
.
修复:
在您的情况下,您可以修改 Block.hpp
中的方法签名以使用对 BasicRobotFunctions
类型的(可能是常量)引用,然后向前声明该类型。这允许您消除对 BasicRobotFunctions.hpp
头文件的依赖。 (Block.cpp
可能需要包括 Block.hpp
和 BasicRobotFunctions.hpp
。)
//...
#include "typedefinitions.h"
#include "dynamixel.h"
class BasicRobotFunctions; // Forward declaration
//...
Block(const BasicRobotFunctions &basic);
//...
int lookAround(const BasicRobotFunctions &basic);
//...
您将来可以通过尽量减少编译头文件所需的头文件来避免此问题。这意味着您的头文件应该:
- 对使用的类型使用前向声明。
- 使用引用转发声明的类型。
您可以通过确保头文件自行编译来检查您的头文件是否已将其依赖性降至最低。我通过首先将头文件包含在相应的源文件中来完成此操作,然后确保源文件编译。
// Foo.cpp
#include "Foo.hpp"
//...
那么你可以在 class 之前提出一个前向声明 as
class BasicRobotFunctions;
class Block {
public:
bool grounded;
//TODO position is a ...
但是这种错误意味着#include "BasicRobotFunctions.hpp"
不要声明 BasicRobotFunctions。代码守卫可能有问题吗?
循环包含可以使用前向声明来解决,在 headers 中放置正确的守卫并将一些包含移动到源文件中。
- 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.
以上内容摘自另一个类似问题,我发现这些点很有用,但 none 确实解决了我的问题,声明如下:
我正在为机器人创建一个自然语言处理器,为软件提供各种对象来表示其环境中的真实世界项目(目前为 Blocks 世界),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_ */
源文件:
/*
* 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;
}
然而,我从构造函数和 turnAround(BasicRobotFunctions) 方法收到以下编译错误:
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
检查了我的其他 classes 使用对象作为变量我得到了同样的错误。 针对引用中的要点: - BasicRobotFunctions.hpp 包括在内 - class 名称在所有提及它的不同实例中拼写相同 - 我没有复制粘贴任何包含保护 - 我没有在项目中使用任何命名空间 - 我也没有使用任何模板 - 文件名在我的包含中没有拼错 - 我没有在程序中定义任何宏 - 我确保每个 class 都在其自己的头文件
中定义我的系统是否可能存在任何其他问题、我犯的任何错误或我正在做的任何错误的编程实践?
您的问题原因:
你有一个头文件循环依赖问题。
main.cpp
包括 BasicRobotFunctions.hpp
其中包括 Robot.hpp
其中包括 Block.hpp
其中包括 BasicRobotFunctions.hpp
.
如果您的头文件被适当地防止多重包含(看起来确实如此),Block.hpp
将看不到 BasicRobotFunctions.hpp
的定义,因为它已经在包含的中间它。
如何发现问题:
此问题的根源在编译错误消息和您的 Block.hpp
文件中很明显。
编译器在 Block.hpp
中报告错误,它逐行描述它是如何通过包含到达该文件的。 Block.hpp
文件的来源清楚地表明它正在尝试包含 BasicRobotFunctions.hpp
.
修复:
在您的情况下,您可以修改 Block.hpp
中的方法签名以使用对 BasicRobotFunctions
类型的(可能是常量)引用,然后向前声明该类型。这允许您消除对 BasicRobotFunctions.hpp
头文件的依赖。 (Block.cpp
可能需要包括 Block.hpp
和 BasicRobotFunctions.hpp
。)
//...
#include "typedefinitions.h"
#include "dynamixel.h"
class BasicRobotFunctions; // Forward declaration
//...
Block(const BasicRobotFunctions &basic);
//...
int lookAround(const BasicRobotFunctions &basic);
//...
您将来可以通过尽量减少编译头文件所需的头文件来避免此问题。这意味着您的头文件应该:
- 对使用的类型使用前向声明。
- 使用引用转发声明的类型。
您可以通过确保头文件自行编译来检查您的头文件是否已将其依赖性降至最低。我通过首先将头文件包含在相应的源文件中来完成此操作,然后确保源文件编译。
// Foo.cpp
#include "Foo.hpp"
//...
那么你可以在 class 之前提出一个前向声明 as
class BasicRobotFunctions;
class Block {
public:
bool grounded;
//TODO position is a ...
但是这种错误意味着#include "BasicRobotFunctions.hpp" 不要声明 BasicRobotFunctions。代码守卫可能有问题吗? 循环包含可以使用前向声明来解决,在 headers 中放置正确的守卫并将一些包含移动到源文件中。