unable to make default constructor for class with reference as member variable

I facing some issue in below code
...

1
2
3
4
5
6
7
8
9
10
11
12
13
 class Temp {
                private :
                        string &s ;
                        int index;
                public :
                        Temp():index(0) {}   
                       Temp(string &Ls) :s(Ls),index(0){}
                       Temp& Temp::operator=(const Temp&){
				   this->s =Temp.s;
				   this ->index = Lst.index;
				   return *this ;
			}
	};


while compiling I’m getting the error message as

error: uninitialized reference member `temp::s'

In the main function call I can able to declare as

Temp it("test");

but I want declare like this

Temp it ;

can anyone suggest some other way to sort out this problem.
Last edited on
You need to initialise reference memebers in the initialisation list - they cannot be left unitialised (as you have found).

So:
1. Do without a default constructor - (could be a problem in certain circumstances);

2. Don't have reference members.



But iterator will have the reference to the container but we can able to declare the iterator without assign to any container

string::iterator it;

in this case how reference object should initialized since above will call zero argument on constructor.
You are confused about something.

Iterators are classes and they are defined as a nested class of the type they iterate on.

Something along this (very) general lines
1
2
3
4
5
6
7
8
9
10
class string
{
     public:
      class string_random_iterateor
      {
           
       };

      typedef string_random_iterator iterator;
};



So you can say string::iterator it; //created a string iterator variable

iterators behave like pointers NOT references

They are several good books on the STL out there if you are interested in further reading.


So in your class - I would say - get rid of that parameter as a reference and just have string s
Last edited on
It’s ok I explain my entire program....
I’m creating a container type class contain the array of string and a iterator class to access it.

my header files as below :-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
//string.h
#include <string>
#include <iostream>
using namespace std;

class Lstring {
        private :
                string s[100];
                int count;
        public :
                Lstring();
                Lstring( const Lstring&);
                ~Lstring();

                const char* operator [] (int i)const ;
                int push_back(const string );
                void print()const;


        //class iterator;
        //friend class iterator ;
        class iterator {
                private :
                        Lstring &s ;
                        int index;
                public :
                        //iterator();
                        iterator(Lstring &);
                        iterator(Lstring&,bool);
                        iterator& iterator::operator=(const iterator&);
                        ~iterator();
                        iterator& operator ++(int );
                        friend ostream& operator<<(ostream& os, const iterator& it);



        };
                        iterator  begin();
                        iterator  end() ;
};


and main function is
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//main.cpp
int main()
{
        Lstring Lst;
        Lst.push_back("Test1");
        Lst.push_back("Test2");
        Lst.print();
        Lstring L2 = Lst;
        L2.print();
//		Lstring::iterator it
        Lstring::iterator it =  Lst.begin();
        cout << endl << " in the Begin Iterator"<<it<<endl;
        it++;
        cout << endl << " in the Incrementing Iterator"<<it<<endl;
        it = Lst.end();
        cout << endl << " in the End Iterator"<<it <<endl;
        return 0;
}


i have wrttien this program with refernce to the thinking in cpp volume 1 (chapter Introduction to Templates - page 764) book.

here if want to declare the iterator alone without assiging to the any container(Lstring) class. then how i have to code the zero argument constructor of iterator class.

if pointer are used for references how iterator class implementated to access the container oject.
But you will notice that the book does not attempt to have a default constructor.

So they don't do IntStackIter it; as you are trying to do - they doIntStackIter it(is); - they realise the problem.

If you want to have a default constructor then use pointers instead of references.


Last edited on
thanks.. for suggestion ...

In that program i have a question i.e use of declaring the iterator class as friend.

friend class iterator ;
The StackInt class has some private variables:
1
2
3
4
class IntStack {
enum { ssize = 100 };
int stack[ssize];
int top;


The iterator needs access to these private variables:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
// 
class IntStackIter {
IntStack& s;
int index;
public:
IntStackIter(IntStack& is) : s(is), index(0) {}
int operator++() { // Prefix
require(index < s.top,//<<=====Iterator class accesses private member of StackInt class
"iterator moved out of range");
return s.stack[++index]; //<<===== and here
}
int operator++(int) { // Postfix
require(index < s.top, //<<===== and here
"iterator moved out of range");
return s.stack[index++];//<<===== and here
}
};


So the StackInt class declares the IntStackIter iterator class to be a friend - to allow it to access the private members.
Topic archived. No new replies allowed.