“对象”尚未声明错误
我是来自 Java 的 C++ 新手,所以我不确定我在这里做错了什么 person.h
中的以下行给了我错误Transaction has not beclared
。
void pay(Transaction transaction);
我有一个 Transaction 对象,我是否必须在 person.h 文件中的某个位置声明/包含它?
这是我的 person.h
源代码
#ifndef PERSON_H_
#define PERSON_H_
#include "Transaction.h"
using std::string;
class Person {
public:
class Transaction;
Person();
virtual ~Person();
void pay(Transaction* transaction);
};
#endif /* PERSON_H_ */
I'm new to c++ coming from Java so I'm not sure what I'm doing wrong here
the following line in person.h
is giving me the error Transaction has not been declared
.
void pay(Transaction transaction);
I have a Transaction object, do I have to declare / include it in the person.h
file somewhere?
here is my person.h
source
#ifndef PERSON_H_
#define PERSON_H_
#include "Transaction.h"
using std::string;
class Person {
public:
class Transaction;
Person();
virtual ~Person();
void pay(Transaction* transaction);
};
#endif /* PERSON_H_ */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
在 C++ 中,将声明与定义分开是一件大事。声明告诉如何使用代码,并且位于头文件 (
.h
) 中。定义是代码本身,位于源 (.cpp
) 文件中。如果文件仅通过指针或引用引用
Transaction
(如标头中常见的情况),则只需使用class Transaction;
预声明事务类。如果文件需要实际的 Transaction 值,则需要#include "Transaction.h"
。如果它仍然说
Transaction
是一个未定义的符号,这意味着你有一个循环依赖:一系列在循环中相互包含的标头。在这种情况下,您需要将其中一个头文件更改为仅使用指针和引用,并在其他头文件中预先声明类。In C++, seperating the declarations from the definitions is a big thing. Declarations tell how to use the code, and are in header (
.h
) files. Definitions are the code itself, and are in source (.cpp
) files.If a file only refers to
Transaction
by pointer or reference (as is common in a header), then you only need to predeclare the transaction class, withclass Transaction;
. If the file needs an actual Transaction value, then you need to#include "Transaction.h"
.If it still says
Transaction
is an undefined symbol, that means you have a circular dependency: A series of headers that include each other in a loop. In that case, you need to alter one of the header files to only use pointers and references, and predeclare the classes in the other headers.当您在另一个类中声明一个类时,您就声明了一个内部类。
因此
声明一个类,其完全限定名称为 Person::Transaction。即使在全局范围内还有另一个名为 Transaction 的类,对 Transaction 的引用也将被假定为引用 Person::Transaction。由于您还没有定义该类,因此您会收到错误。
您想要的是 ::Transaction,无论是包含标头还是在类声明之外进行前向声明。执行
OR
前者仅允许通过引用传递。按值传递或调用函数需要 include 提供的完整类定义。
When you declare a class inside another class, you're declaring an internal class.
So
Declares a class whose fully qualified name is Person::Transaction. Even if there is another class named Transaction at global scope, references to Transaction will be assumed to refer to Person::Transaction. Since you haven't defined that class, you get your error.
What you want is ::Transaction, either from including the header or from forward declaring outside your class declaration. Do either
OR
The former only allows passing by reference. Passing by value or calling a function requires the full class definition provided by include.
我可以想到(至少)两个可能的原因:
Transaction
的前向声明应该在Person
中吗?这意味着Transaction
是Person
中的嵌套类。I can think of (at least) two possible causes for this:
Transaction
is supposed to be inPerson
? That would meanTransaction
is a nested class inPerson
.您必须包含定义 Transaction 的头文件(如果您使用指针,那么您可以在 h 文件中转发声明 Transaction 类,并仅在实现文件中包含 Transaction.h)。
you have to include the header file where the Transaction is defined (if you would have used a pointer then you could just forward declare the Transaction class in your h file and include the Transaction.h only in the implementation file).
文件顶部的
#include "Transaction.h"
应该可以解决问题。#include "Transaction.h"
at the top of your file should do the trick.感谢您发布更多代码。试试这个:
class Transaction
#include "Transaction.h"
希望这有帮助
Thanks for posting more code. Try this:
class Transaction
#include "Transaction.h"
Hope this helps