字符串数组在c++中不能正常工作?

时间:2021-12-07 21:28:31

I'm working on a program for class that takes in a number from 0 to 9999, and spits out the word value (ie 13 would be spit out as "thirteen", etc) And I'm having a pain with the array for some reason.

我正在为一个类编写一个程序,它接收一个从0到9999的数字,并输出单词value(比如13会被输出为“13”等等),我对这个数组感到痛苦。

Here is the class so far:

这是目前为止的课程:

#include<iostream>
#include<string>

using namespace std;

class Numbers
{
    private:

        int number;

        string lessThan20[ ] = {"zero", "one", "two", "three", "four", "five", 
            "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
            "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};
        string incrementsOfTen[ ] = {"twenty", "thirty", "fourty", "fifty", "sixty",
            "seventy", "eighty", "ninety"};
        string hundred = "hundred";
        string thousand = "thousand";

    public:
        Numbers(int newNumber)
        {
            setNumber(newNumber);
        }

        void setNumber(int newNumber)
        {
            if(newNumber < 0 || newNumber > 9999)
            {
                cout << "Number cannot be negative or larger than 9999, defaulted to zero." << endl;
                number = 0;
            }
            else
            {
                number = newNumber;
            }
        }

        int getNumber()
        {
            return number;
        }
};

My issue is up at the two string arrays, I'm getting these errors back from my compiler:

我的问题出在两个字符串数组上,我从编译器中得到这些错误:

1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(19) : error C2059: syntax error : '{'
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(19) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(22) : error C2059: syntax error : '{'
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(22) : error C2334: unexpected token(s) preceding '{'; skipping apparent function body
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(24) : error C2864: 'Numbers::hundred' : only static const integral data members can be initialized within a class
1>f:\college work\csis 297\assignment 4\chapter 11-1\chapter 11-1\11-1.cpp(25) : error C2864: 'Numbers::thousand' : only static const integral data members can be initialized within a class

I'm sure if I spend more time on the single variable strings I can figure those out, but I've spent the better part of an hour looking up what I'm doing wrong on the two arrays, any advice or information would be appreciated.

我确信,如果我花更多的时间在单变量字符串上,我可以计算出来,但是我花了一个小时的时间来查找我在这两个数组上做错了什么,任何建议或信息都会被欣赏。

Thanks in advance.

提前谢谢。

PS: No, there is no main, haven't gotten there yet, my program has a blank main right now as I'm simply trying to get the class to compile error free for now.

没有,没有main,还没有到那里,我的程序现在有一个空白的main,我只是想让这个类暂时编译错误。

EDIT for clarification:

编辑说明:

The first two errors are on the first array, the second two errors on the second array, and the last two errors are on the non-array strings.

前两个错误在第一个数组中,第二个错误在第二个数组中,最后两个错误在非数组字符串上。

2 个解决方案

#1


5  

The error message says it all:

错误信息说明了一切:

only static const integral data members can be initialized within a class

只有静态const集成数据成员才能在类中初始化

You cannot do what you want, you have to separate declaration and initialization, and move the initialization either to the constructor, or use a static const, and put the initialization outside the class. This is preferred, since the values don’t actually change, or depend on an instance:

您不能做您想做的事情,您必须分离声明和初始化,并将初始化移动到构造函数,或者使用静态const,并将初始化放在类之外。这是首选的,因为值实际上没有变化,或者依赖于实例:

class Numbers
{
    private:
        static const string lessThan20[];
        // …
};

string const Numbers::lessThan20[ ] = {"zero", "one", "two", "three", "four", "five", 
            "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
            "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

And analogously for the other array.

对另一个数组也是如此。

#2


1  

I would make your arrays be arrays of const char*. Then use std::string to build your result strings. The C++ string class accepts char* for almost every operation.

我将使你的数组是const char*的数组。然后使用std: string来构建结果字符串。在几乎所有操作中,c++ string类都接受char*。

A string literal like "this is a string" is a const char*. There is no real need to make it into a string.

字符串字面意思是“这是一个字符串”是一个const char*。没有必要把它变成字符串。

And oh yes, as the other answer says, you must define the values of your arrays outside of the class definition.

哦,是的,正如另一个答案所说,你必须在类定义之外定义数组的值。

#1


5  

The error message says it all:

错误信息说明了一切:

only static const integral data members can be initialized within a class

只有静态const集成数据成员才能在类中初始化

You cannot do what you want, you have to separate declaration and initialization, and move the initialization either to the constructor, or use a static const, and put the initialization outside the class. This is preferred, since the values don’t actually change, or depend on an instance:

您不能做您想做的事情,您必须分离声明和初始化,并将初始化移动到构造函数,或者使用静态const,并将初始化放在类之外。这是首选的,因为值实际上没有变化,或者依赖于实例:

class Numbers
{
    private:
        static const string lessThan20[];
        // …
};

string const Numbers::lessThan20[ ] = {"zero", "one", "two", "three", "four", "five", 
            "six", "seven", "eight", "nine", "ten", "eleven", "twelve", "thirteen", 
            "fourteen", "fifteen", "sixteen", "seventeen", "eighteen", "nineteen"};

And analogously for the other array.

对另一个数组也是如此。

#2


1  

I would make your arrays be arrays of const char*. Then use std::string to build your result strings. The C++ string class accepts char* for almost every operation.

我将使你的数组是const char*的数组。然后使用std: string来构建结果字符串。在几乎所有操作中,c++ string类都接受char*。

A string literal like "this is a string" is a const char*. There is no real need to make it into a string.

字符串字面意思是“这是一个字符串”是一个const char*。没有必要把它变成字符串。

And oh yes, as the other answer says, you must define the values of your arrays outside of the class definition.

哦,是的,正如另一个答案所说,你必须在类定义之外定义数组的值。