仅解析“静态const整数数据成员可以在类”编译错误 [英] Resolving "only static const integral data members can be initialized within a class" compilation error

查看:192
本文介绍了仅解析“静态const整数数据成员可以在类”编译错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下用于创建全局对象的操作会导致编译错误。

The following for creating a Global Object is resulting in compilation 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. 如何创建一个全局类
    对象正确地像我试过的$ b $

  2. 如何修复只有静态
    const整数数据成员可以是
    在类中初始化

  3. 基本上如何解决其余的
    的错误,所以我可以得到
    compile?

我喜欢在文件范围声明全局类对象(我喜欢在文件范围声明所有全局变量),因为当我必须创建单独的源文件并执行extern和一切变得非常复杂,从来没有为我工作。虽然,我想知道如何做到最终...似乎每个教程我看不会编译,除非它编译我不知道如何重新创建它!

I like declaring Global Class Objects at file scope (I like declaring all globals at file scope) because when I have to create separate source files and do "extern" and everything it becomes extremely complicated and never works for me. Although, I do want to figure out how to do that eventually... it seems every tutorial I look at won't compile though and unless it compiles I have no idea how to recreate it!

如果我可以让这个编译...然后我可以成功学习如何做到这一点。所以如果有人可以重写上面的字面上复制&粘贴到Visual C ++ Express 2008和作品我终于能够找出如何重新创建它。我非常兴奋,看到这个修复!它只是我不能得到全局对象工作正常!有关声明全局类对象...或任何其他信息的任何其他信息欢迎!

If I can just get this to compile...then I can successfully learn how to do this. So if someone could rewrite the above to where it literally copies & pastes into Visual C++ Express 2008 and works I will finally be able to figure out how to recreate it. I'm extremely excited on seeing the fix for this! It is just I can't get Global Objects to work right! Any other information on declaring Global Class Objects...or anything for that matter is welcome!

推荐答案

一个。很多错误只是从初始错误中级联,所以看起来像有很多问题,当只有一对夫妇。只需从顶部开始:

Just start addressing the errors one by one. A lot of the errors are just cascaded from the initial errors, so it looks like there are a lot of problems when there's only a couple. Just start from the top:

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

您不能初始化成员在类定义中,除非它是static,const和一个整数类型。离开 number 的声明中的 = 5 。然后,您需要在类定义之外定义 Tester :: number ,如下所示:

You can't initialize a member in the class definition unless it's static, const, and one of the integral types. Leave the "= 5" off of the declaration of number. Then you'll need to have a definition of Tester::number outside of the class definition, like so:

int Tester::number = 5;

问题#2:

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

几乎正是它所说的(缺少分号错误可能在说分号应该在哪里有点不精确) - 你需要一个分号在 Tester class。

Almost exactly what it says (missing semi-colon errors can be a bit inexact in saying where the semicolon should be) - you need a semi-colon after the definition of the Tester class.

修复这些问题,您的编译问题就会消失。

Fix those and your compilation problems go away.

关键是从顶部每次尝试一个编译器错误。如果你得到超过3个,你可能只是忽略一切后第3左右,因为初始错误只是导致编译进入杂草(如果他们是真正的错误,他们会再次显示在下一个编译反正)。

The key thing is to try and take compiler errors one at a time from the top. If you get more than about 3 of them, you can probably just ignore everything after the 3rd or so because the initial error just cause the compile to into the weeds (and if they are real errors, they'll show up again in the next compile anyway).

这篇关于仅解析“静态const整数数据成员可以在类”编译错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆