解决“只能在类中初始化静态const积分数据成员”编译错误

时间:2009-01-31 07:12:08

标签: c++ visual-studio compiler-errors

以下创建全局对象导致编译错误。

#include "stdafx.h" 
#include <iostream> 

using namespace System; 
using namespace std;    
#pragma hdrstop 

class Tester;


void input();

class Tester
{
    static int number = 5;

public:

    Tester(){};
    ~Tester(){};

    void setNumber(int newNumber)
    {
        number = newNumber;
    }

    int getNumber()
    {
        return number;
    }
}

Tester testerObject;

void main(void)
{
    cout << "Welcome!" << endl;

        while(1)
        {
            input();
        }
}

void input()
{
    int newNumber = 0;

    cout << "The current number is " << testerObject.getNumber();
    cout << "Change number to: ";

        cin >> newNumber;

    cout << endl;

    testerObject.setNumber(newNumber);

    cout << "The number has been changed to " << testerObject.getNumber() << endl;
}

以下是编译错误:

1>------ Build started: Project: test, Configuration: Debug Win32 ------
1>Compiling...
1>test.cpp
1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class
1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject'
1>.\test.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>.\test.cpp(49) : error C2039: 'getNumber' : is not a member of 'System::Int32'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>.\test.cpp(55) : error C2039: 'setNumber' : is not a member of 'System::Int32'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>.\test.cpp(57) : error C2039: 'getNumber' : is not a member of 'System::Int32'
1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
1>Build log was saved at "file://c:\Users\Owner\Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm"
1>test - 6 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
  1. 如何创建全局类 像我试过的那样对象正确 这里。
  2. 我该如何修复“只有静态 const积分数据成员即可 在类“
  3. 中初始化
  4. 基本上我该如何解决剩下的问题 的错误,所以我可以得到这个 编译?
  5. 我喜欢在文件范围内声明全局类对象(我喜欢在文件范围内声明所有全局变量),因为当我必须创建单独的源文件并执行“extern”时,它变得非常复杂并且永远不适合我。虽然,我确实想知道如何最终做到这一点......似乎我看到的每个教程都不会编译,除非它编译我不知道如何重新创建它!

    如果我可以让它编译...那么我可以成功地学习如何做到这一点。因此,如果有人可以将上述内容重写为字面上复制的内容。粘贴到Visual C ++ Express 2008并且工作我终于能够弄清楚如何重新创建它。看到修复此问题,我感到非常兴奋!只是我无法让Global Objects正常工作!有关声明Global Class Objects的任何其他信息......或者其他任何相关信息都是受欢迎的!

4 个答案:

答案 0 :(得分:11)

开始逐一解决错误。很多错误只是从最初的错误中级联出来,所以当只有一对错误时,看起来有很多问题。从顶部开始:

1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class

除非是static,const和其中一个整数类型,否则不能在类定义中初始化成员。将“= 5”保留为number的声明。然后你需要在类定义之外定义Tester::number,如下所示:

int Tester::number = 5;

问题#2:

1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject'

几乎完全是它所说的(缺少分号错误在说分号应该在哪里可能有点不精确) - 在定义Tester类之后你需要一个分号。

修复这些问题,编译问题就消失了。

关键是尝试从顶部一次一个地编译错误。如果你得到超过3个左右,你可能会在第3个左右之后忽略所有内容,因为初始错误只会导致编译进入杂草(如果它们是真正的错误,它们将在下一次出现无论如何都要编译。)

答案 1 :(得分:1)

  • 错误C2864:向您的整数添加const修饰符,或将初始化移到类外(如class Tester { static int number; }; int Tester::number = 5;中所示)。后者似乎更适合你的情况。
  • 错误C2146:声明class Tester { ... }后,您错过了分号。它应该是class Tester { ... } ;

其他错误可能是由上一个错误引起的。它们应该在修复后自动修复。

作为旁注,我认为你真的不想在你的成员上使用static修饰符。它似乎更适合实例字段。你仍然无法就地初始化它(这不是C#),你必须将初始化移动到构造函数。例如:

class Tester {
    int number;
    static int staticNumber; // just to show you how to use a static field

public:
    Tester() : number(5) {}
    ~Tester() {} // I suggest you remove the destructor unless you need it

    int getNumber() { return number; }
    void setNumber(int value) { number = value; }

    static int getStaticNumber() { return staticNumber; }
    static void setStaticNumber(int value) { staticNumber = value; }
};

// initialize static members *outside* the class
int Tester::staticNumber = 5;

答案 2 :(得分:0)

根据这个:http://publib.boulder.ibm.com/infocenter/lnxpcomp/v8v101/index.jsp?topic=/com.ibm.xlcpp8l.doc/language/ref/cplr038.htm

Tester testerObject;
int Tester::number = 5;

我不是肯定的,但我认为其余的错误来自那个问题。 解决这个问题,看看它能带给你多少。

答案 3 :(得分:0)

这里的答案处理了为什么你的代码没有编译以及如何纠正它。但是我对你对“extern”的评论很感兴趣。当你知道如何使用它很容易。你在一个标题中声明了extern'ed变量。然后在一个文件中初始化它。任何其他文件都可以通过包含标题来引用变量。 e.g。

header.h:

// ensure the file is only included once
#ifndef _HEADER_H
#define _HEADER_H

extern int foo;

#endif
// end file header.h

header.cpp

#include "header.h"

int foo = 1;

// end file header.cpp

的main.cpp

#include "header.h"
#include <stdio.h>

int main(int argc, char** argv)
{
  printf("%d", foo);
  return 0;
}

// end file main.cpp

虽然对全局变量使用静态类成员有助于适应oo设计方案,但它比必要的更精细。如果你不必严格遵守oo,只需使用extern,它更容易,代码更少。