“对象”尚未声明错误

发布于 2024-12-11 01:04:04 字数 537 浏览 1 评论 0原文

我是来自 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

假扮的天使 2024-12-18 01:04:04

在 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, with class 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.

灯角 2024-12-18 01:04:04

当您在另一个类中声明一个类时,您就声明了一个内部类。

因此

class Person {
public:

    class Transaction;
};

声明一个类,其完全限定名称为 Person::Transaction。即使在全局范围内还有另一个名为 Transaction 的类,对 Transaction 的引用也将被假定为引用 Person::Transaction。由于您还没有定义该类,因此您会收到错误。

您想要的是 ::Transaction,无论是包含标头还是在类声明之外进行前向声明。执行

class Transaction;
class Person {
};

OR

#include "Transaction.h"
class Person
{
};

前者仅允许通过引用传递。按值传递或调用函数需要 include 提供的完整类定义。

When you declare a class inside another class, you're declaring an internal class.

So

class Person {
public:

    class Transaction;
};

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

class Transaction;
class Person {
};

OR

#include "Transaction.h"
class Person
{
};

The former only allows passing by reference. Passing by value or calling a function requires the full class definition provided by include.

终陌 2024-12-18 01:04:04

我可以想到(至少)两个可能的原因:

  • 您确定 Transaction 的前向声明应该在 Person 中吗?这意味着 TransactionPerson 中的嵌套类。
  • 头文件是否相互包含,即是否存在循环依赖?

I can think of (at least) two possible causes for this:

  • Are you sure the forward declaration of Transaction is supposed to be in Person? That would mean Transaction is a nested class in Person.
  • Are the header files including each other, i.e. is there a cyclic dependency?
弃爱 2024-12-18 01:04:04

您必须包含定义 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).

燃情 2024-12-18 01:04:04

文件顶部的 #include "Transaction.h" 应该可以解决问题。

#include "Transaction.h" at the top of your file should do the trick.

过气美图社 2024-12-18 01:04:04

感谢您发布更多代码。试试这个:

  1. 在 person.h 文件的顶部放置: class Transaction
  2. 在 person.cpp 文件的顶部放置: #include "Transaction.h"

希望这有帮助

Thanks for posting more code. Try this:

  1. In the person.h file, at the top put: class Transaction
  2. In the person.cpp file, at the top put: #include "Transaction.h"

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文