创建向量和向量元素的引用 [英] Creating references to vectors and elements of vectors

查看:79
本文介绍了创建向量和向量元素的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么以下工作没有评论?从引用中获取引用是禁止的吗?或者我忽略了一些简单的事情?

Why doesn't the following work where commented? Getting references from references is verboten? Or am I overlooking something simple?

#include <vector>
#include <string>

using namespace std;

class Strings {
   private:
      vector<string> stringsC;

   public:
      Strings() {
         stringsC.push_back("1");
         stringsC.push_back("2");
         stringsC.push_back("3");
         }

      const string & str(int pos) { return stringsC[pos]; }
      const vector<string> & vecRef() { return stringsC; }
      const string & operator [](int pos) { return stringsC[pos]; }
   };

class StringHolder {
   private:
      Strings stringsC;

   public:
      StringHolder() {
         const string & str = stringsC[0];  //Works
         }
      const string & getElement(int pos) {
         return stringsC.str(pos);
         }
      const Strings & getVec() { return stringsC; }
   };


 
int main() {
   StringHolder sh;
   const string & s = sh.getElement(1);   //Works
   const Strings & strings = sh.getVec(); //Works

   const string s1 = strings.vecRef[0];   //No Go
   //The previous gives: Error	C3867	'Strings::vecRef': non-standard syntax;
   //use '&' to create a pointer to member

   const string & s2 = strings[0];        //No Go
   //The previous gives: Error C2678 binary '[': no operator found which takes a
   //left-hand operand of type 'const Strings' (or there is no acceptable conversion)

   return 0;
   }





我的尝试:



前面的代码及其扰动。



What I have tried:

The previous code, and perturbations thereof.

推荐答案

const string s1 = strings.vecRef[0];   //No Go



看看 vecRef 的定义,它是一个返回对向量的引用而不是astring的函数。


Look at the definition for vecRef, it is a function that returns a reference to a vector, not astring.

const string & s2 = strings[0];        //No Go



字符串的定义是对字符串对象的常量引用,而不是数组。



至于其余的我会偷偷摸摸地想弄清楚弦乐,弦乐,弦乐C等之间的区别。







我怀疑我有垃圾箱

[/ edit]


The definition of strings is a constant reference to a Strings object, not an array.

As to the rest I am going cross-eyed trying to figure out the difference between Strings, strings, stringsC etc.


[edit]
I suspect "I have bin took"
[/edit]


Gaagh!理查德帮助指明了方向。对于之后的任何人来说,根本的错误是忘记双重指定'const'。以下是工作代码,希望没有前面的交叉眼睛:

Gaagh!!! Richard helped point the way. For anyone who comes after, the fundamental error was forgetting to double-specify 'const'. The following is working code, hopefully without the cross-eyed-ness of the previous:
#include <vector>
#include <string>

using namespace std;

class NumbersAsStrings {
   private:
      vector<string> stringsC;

   public:
      NumbersAsStrings() {
         stringsC.push_back("1");
         stringsC.push_back("2");
         stringsC.push_back("3");
         }

      const string & number(int pos) const { return stringsC[pos]; }
      const vector<string> & vecRef() const { return stringsC; }
      const string & operator [](int pos) const { return stringsC[pos]; }
   };

class ComposedClass {
   private:
      NumbersAsStrings numbersC;

   public:
      ComposedClass() {
         const string & str = numbersC[0];
         }
      const string & getElement(int pos) {
         return numbersC.number(pos);
         }
      const NumbersAsStrings & getStrings() { return numbersC; }
   };


 
int main() {
   ComposedClass composed;
   const string & s = composed.getElement(1);
   const NumbersAsStrings & strings = composed.getStrings();

   const string & s1 = strings.vecRef()[0];

   const string & s2 = strings[0];

   return 0;
   }


这篇关于创建向量和向量元素的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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