Click here to Skip to main content
15,880,469 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Why doesn't the following work where commented? Getting references from references is verboten? Or am I overlooking something simple?
C++
#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.
Posted
Updated 10-Oct-18 10:48am

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

Look at the definition for vecRef, it is a function that returns a reference to a vector, not astring.
C++
const string & s2 = strings[0];        //No Go

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]
 
Share this answer
 
v2
Comments
David O'Neil 10-Oct-18 16:00pm    
>>const string s1 = strings.vecRef[0]; //No Go

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

As 'vecRef' does return a reference to a vector, can't you use operator[] on that reference to get at the individual strings? That is what has me perplexed. (Unless, somehow, the compiler is saying that the vector and strings are private to the originating class somehow. But the error code doesn't make sense in those terms.)
David O'Neil 10-Oct-18 16:51pm    
Don't know why you think you've been took, but thanks for the pointer. -5- for it - it helped get me to the answer I posted below. Thanks!
Richard MacCutchan 11-Oct-18 3:35am    
I thought that because of your comment to me in the Lounge. On reflection the above was a serious (and sensible) question.
David O'Neil 11-Oct-18 3:48am    
As you can tell, I was seriously stumped until after your post. The compiler error wasn't helpful at all to me, especially since the whole section of code seemed to work previously, except I had to move a definition out of the header and into the cpp file! You didn't solve it, but you certainly helped speed me towards the solution - thanks!
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:
C++
#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;
   }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900