1
我正在构建一个秘密消息类,它具有消息中的行向量和每个消息可以查看的最大次数。我试图重载[]运算符以便能够看到消息。将const引用返回给字符串
例如:如果我想初始化字符串以下载体,我应该能够做到......
vector<string> m = {
"Here is the first line",
"I have a second line as well",
"Third line of message"};
//initialize message - each line may be viewed a maximum of two times
SelfDestructingMessage sdm(m, 2);
cout << sdm[0] << endl;
//outputs "Here is the first line" and decrements remaining views of first line by one
我的问题是,我宣布在头文件的操作,然后将它定义在功能文件如下:
string SelfDestructingMessage::operator[](size_t index){
return const string & message[index];
}
因此,我应该能够看到使用[]运算符,它取为size_t参数(索引)的实际消息。它应该返回一个const引用,该引用是从特定于该对象的消息向量中索引的消息字符串。
但是,编译时,我得到一个“错误:在'const'返回const字符串之前期望的primary-expression & message [index];”
关于这个原因的任何想法?