【问题标题】:Class header files类头文件
【发布时间】:2011-12-12 22:19:01
【问题描述】:

main.cpp

int main()
{
return 0;
}

细胞.h

#pragma once

class _cell {};

细胞.cpp

#include "cell.h"

实验.h

#pragma once

class _experiment
{
_cell cell;
};

experiment.cpp

#include "experiment.h"
#include "cell.h"

错误:

experiment.h(5): error C2146: syntax error : missing ';' before identifier 'cell'
experiment.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
experiment.h(5): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

这让我发疯了。有什么帮助吗?谢谢!

【问题讨论】:

  • 通常您只希望看到编译器定义的符号以下划线开头。
  • 我总是使用 _type 作为类型。感谢您的提示,从现在开始将尝试使用其他东西

标签: c++ windows visual-studio


【解决方案1】:

您必须从experiment.h#include <cell.h>,否则_cellexperiment.h 中使用时未定义。

【讨论】:

    【解决方案2】:

    将包含从 cell.cpp 移动到 .h

    .h:

    #pragma once
    #include "cell.h"
    
    class _experiment
    {
    _cell cell;
    };
    

    .cpp:

    #include "experiment.h"
    

    类需要定义成员来包含它们的实例。

    【讨论】:

    • 我将所有包含从 cpp 文件移动到头文件。然后发现了问题。我有一个“common.h”,其中包括“cell.h”,但在“cell.h”中我还包括“common.h”。所以我在includes中有一个循环。现在完美运行。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-04-08
    • 2014-08-02
    • 2013-11-21
    • 2021-11-24
    • 2010-09-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多