c++ - 在类中分配和使用公共(public)变量

标签 c++

<分区>

我在 C++ .h 类文件中有一个 ABC 类。

.h文件

#ifndef ABC_H_
#define ABC_H_

class ABC
{
  public:
    int x;
    int y;
};
#endif

.cpp文件

//----- Empty  -----------

主程序.cpp

#include <iostream>
#include "ABC.h"
using namespace std;
    int main() {
      ABC a1;
      a1.x=5; a1.y=2;
      cout<<a1.x;
      // ...
    }

在 Eclipse 中编译时出错:

symbols not found for architecture x86_64

ld: symbol(s) not found for architecture x86_64
collect2: ld returned 1 exit status
make: *** [CPPProgram] Error 1

所有其他的 hello world 程序等都可以正常编译和运行。我记得当我在使用模板时在单独的文件中进行定义和实现时遇到了这个错误(当我在同一个文件中实现定义和实现时它消失了)

我不确定这里出了什么问题。有什么建议吗?

最佳答案

在您发布的代码中,您缺少一个分号:

class ABC
{
public:
int x;
int y;
};
 ^

关于c++ - 在类中分配和使用公共(public)变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10934507/

相关文章:

c++ - 如何避免重复的模板特化?

c++ - VS2019 : How to resolve the "unknown sub-lang: 0x8" message in Resource View?

C++ gRPC线程数配置

c++ - 从轮廓绘制垫子?

c++ - string::substr 中无法解释的 out_of_range

c++ - 简单的程序,没有callstack, "inpossible"找错

c++ - Qt : Efficiently handle QGraphicsItems that have "lots of pixmaps"? (RTS)

c++ - 根据第三个整数的符号快速加/减两个整数

c++ - 如何使用类模板特化创建别名模板的特化?

c++ - 如何使 O(n) 的功能检查字母(上和下)和 ()+-*/到尾递归?