如何在C ++中的头文件中声明一个字符串数组 [英] How to declare a string array in a header file in C++

查看:150
本文介绍了如何在C ++中的头文件中声明一个字符串数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在尝试在头文件中声明一个字符串数组但是我收到了这个消息:

字符串没有命名类型



这就是代码:



Hi,
I'm trying to declare a string array in a header file but I receive this msg :
"string does not name a type"

and that's the code :

#ifndef EXCHANGER_H_INCLUDED
#define EXCHANGER_H_INCLUDED
#include <string>

class exchanger
{
public :
    int word_to_number(string word);
    string number_to_word(int number);

private:
    struct node
    {
        string word;
        int number;
        node* p_next;
    };
    string chunks = {"hundred","thousand","million","billion"};
};
#endif // EXCHANGER_H_INCLUDED





我试过用



I tried to use

using namespace std;

在顶部代码,但我已经读过这不是一个好主意,但问题是一样的。

任何建议??

at the top of the code but I have read that's not a good idea, but the problem is the same.
Any suggestions ??

推荐答案

错误与数组无关。您也可以在标头中声明数组,但必须在实现文件中将其初始化。而不是数组为什么不使用矢量。 http://www.cplusplus.com/reference/vector/vector/ [ ^ ]





HEADER

The error is not about the array. Also you can declare the array in header but must initialise it outside in your implementation file. Instead of array why not use vector. http://www.cplusplus.com/reference/vector/vector/[^]


HEADER
#ifndef EXCHANGER_H_INCLUDED
#define EXCHANGER_H_INCLUDED
#include <string>
#include <vector>

class exchanger
{
public :
    int word_to_number(std::string word);
    std::string number_to_word(int number);

private:
    struct node
    {
        std::string word;
        int number;
        node* p_next;
    };
    static const std::vector<std::string> chunks;
};
#endif // EXCHANGER_H_INCLUDED





CPP




CPP

std::vector<std::string> MakeVector()
{
    std::vector<std::string> v;
    v.push_back("hundred");
    v.push_back("thousand");
    //etc
    return v;
}

const std::vector<std::string> exchanger::chunks = MakeVector();


//OR in c++11
//const std::vector<std::string> exchanger::chunks ({"hundred","thousand","million","billion"});


使用 std :: string 而不是字符串摆脱错误。



但你的代码仍然无法编译。您必须使用方括号声明一个数组。因此,请将您的代码更改为:

Use std::string instead of string to get rid of the errors.

But your code will still not compile. You must declare an array using square brackets. So change your code to:
class exchanger
{
public :
    int word_to_number(std::string word);
    std::string number_to_word(int number);

private:
    struct node
    {
        std::string word;
        int number;
        node* p_next;
    };
    std::string chunks[4] = {"hundred","thousand","million","billion"};
};





在运行时不应更改字符串时,最好声明一个静态const数组< br $>



When the strings should not be changed during runtime, it may be better to declare a static const array

class exchanger
{
// ...
private:
    static const std::string chunks[]; 
};



并在源文件中初始化它


and initialise it in the source file

const std::string exchanger::chunks[4] =
    {"hundred","thousand","million","billion"};


您必须符合 std: :每次出现 string 或使用命名空间指令

You must either qualify with std:: every occurrence of string or use the namespace directive
using namespace std;



或者,最后,使用声明


or, finally, the using declaration

using std::string;





请注意:



Please note:

引用:

字符串chunks = {百,千,百万,十亿};

string chunks = {"hundred","thousand","million","billion"};

错了,你是使用初始化列表初始化单个项目,请改为使用



is wrong, you are using a initializer list for initializing a single item, use instead

string chunksi[4] = {"hundred","thousand","million","billion"};









or

string chunksi[4]{"hundred","thousand","million","billion"};


这篇关于如何在C ++中的头文件中声明一个字符串数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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