如何编写数组的setter和getter? (C ++) [英] How do I write setters and getters for an array? (c++)
本文介绍了如何编写数组的setter和getter? (C ++)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在用C ++写一个类,但是我不确定如何为数组创建setter和getter(很抱歉,这是一个基本问题!),我收到以下错误:
Im writing a class within c++, however I am not certain on how to create the setters and getters for the arrays (sorry for it being a basic question!) I am getting the following error:
在']'标记之前的预期主要表达式
expected primary expression before ']' token
这是我的代码:
Class planet: public body
{
private:
string name[];
string star[];
public:
void nameSetter (string h_name[])
{
name[] = h_name[];
}
};
对于这个愚蠢的问题,我再次感到抱歉,我知道我没有传递索引,但是,当我创建索引时,它会引发大量错误!
Once again I am sorry for such I silly question, I know I am not passing an index through, however, when I create an index it throws up a large amount of errors!
推荐答案
string name[];
这不是数组,而是一个指针.使用vectors
代替:
This is not an array, it is a pointer. Use vectors
instead:
#include <vector>
class planet: public body
{
private:
vector<string> name;
vector<string> star;
public:
void nameSetter (const vector<string> &h_name)
{
name = h_name;
}
};
这篇关于如何编写数组的setter和getter? (C ++)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文