Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

accessing vectors embedded in class objects

17 views
Skip to first unread message

Popping mad

unread,
Nov 29, 2016, 1:22:05 AM11/29/16
to
I wanted to embed a vector in my class then access it through a wrapper within the
class that involved the subscript operator operator[]<> and I've run across an
unexpected problem I didn't anticipate

So a Class looks like this, with the data member _ofthings<T> being a vector type.

It has an accessory function ofthings() that returns the vector as an available lvaue

T& ofthings(){
return _ofthings;
};

the overload is vissible and when I compile I get an error
|| g++ -Wall -ggdb -pg -M *.c >make.deps
|| g++ -Wall -ggdb -pg -pg -pthread -o lvalue.exe lvalue.c
|| lvalue.c: In function ‘int main(int, char**)’:
lvalue.c|91 col 14| error: assignment of read-only location ‘test2.vect::Lvalue<T>::operator[]<int>(j)’
|| test2[j] = i;
|| ^
|| lvalue.c: In instantiation of ‘const T& vect::Lvalue<T>::operator[](int) [with T = int]’:
lvalue.c|91 col 10| required from here
lvalue.c|57 col 23| error: subscripted value is neither array nor pointer
|| return ofthings()[index];
|| ~~~~~~~~~~^
|| lvalue.c: In instantiation of ‘T& vect::Lvalue<T>::ofthings() [with T = int]’:
lvalue.c|57 col 21| required from ‘const T& vect::Lvalue<T>::operator[](int) [with T = int]’
lvalue.c|91 col 10| required from here
lvalue.c|44 col 13| error: invalid initialization of reference of type ‘int&’ from expression of type ‘std::vector<int>’
|| return _ofthings;
|| ^~~~~~~~~
make: *** [makefile|5| lvalue] Error 1


namespace vect{

/*
* =====================================================================================
* Class: Lvalue
* Description: testing vector access as an lvalue
* =====================================================================================
*/

template < class T >
class Lvalue
{
public:

// ==================== LIFECYCLE =======================================
Lvalue (){}; /* constructor */
Lvalue (std::vector<T> in): _ofthings{in}{}; /* constructor */
Lvalue ( const Lvalue &other ); /* copy constructor */
~Lvalue (){}; /* destructor */

/* ==================== ACCESSORS ======================================= */
T& ofthings(){
return _ofthings;
};

void ofthings(std::vector<T> const vec){
_ofthings = vec;
};
/* ==================== MUTATORS ======================================= */

/* ==================== OPERATORS ======================================= */

const Lvalue& operator = ( const Lvalue &other ); // assignment operator
const T& operator [] (int index)
{
return ofthings()[index];
};
friend std::ostream &operator << (std::ostream &os, const Lvalue in)
{
for(int i = 0; i < in._ofthings.size(); i++ ){
std::cout << i << "\t";
}
return os;
};

/* ==================== DATA MEMBERS ======================================= */
protected:
std::vector<T> _ofthings;

private:

}; /* ----- end of template class Lvalue ----- */

};


Main is this

int main ( int argc, char *argv[] )
{

std::cout << "TEST INPUT TO VECTOR" << std::endl;
std::vector<int> test {0};
for(int i = 0; i<10; i++ ){
test[i] = i;
std::cout << test[i] << "\t";
}
std::cout << std::endl;
std::cout << "TEST INPUT TO OBJECT" << std::endl;
vect::Lvalue<int> test2 {test};
for(int j=0, i = 10; j<10; i--, j++){
test2[j] = i;
std::cout << test2[j] << "\t";
}
std::cout << std::endl;
std::cout << "__DONE__";



return EXIT_SUCCESS;
} /* ----


So I can not overload [] unless I access a pointer or an array?

Alf P. Steinbach

unread,
Nov 29, 2016, 1:25:56 AM11/29/16
to
On 29.11.2016 07:21, Popping mad wrote:
> [snip]

Generally, start with the very first error message, and ignore the rest.

Figure out what it means.

Fix that error.

Compile.

Repeat.


Cheers & hth.,

- Alf

Popping mad

unread,
Nov 29, 2016, 1:45:35 AM11/29/16
to
On Tue, 29 Nov 2016 07:22:54 +0100, Alf P. Steinbach wrote:

>
> On 29.11.2016 07:21, Popping mad wrote:
>> [snip]
>
> Generally, start with the very first error message, and ignore the rest.
>
> Figure out what it means.
>
> Fix that error.

As usually, Alf, that is the WRONG answer. You're very consistent in being wrong all the time.
But I did fix the problem in that I needed to correct the return type to vector<T> and not T.

Good work Alf. WHo knows, in 20 years you might be able to learn how to be helpful or read compiler errors. Neither is likely.

Alf P. Steinbach

unread,
Nov 29, 2016, 2:17:46 AM11/29/16
to
If this is “Popping mad” then he's suddenly acquired

• very much improved English,
• an ungratefulness and tendency to lie that he's not displayed before, and
• posting via a different server than usual.

I guess he'll clear that up, whether he was impersonated. :)

In any case this was posted by a person who is not very nice.


Cheers!,

_ Alf

ruben safir

unread,
Nov 29, 2016, 2:49:46 AM11/29/16
to
Alf, your off your rocker, truly.


Alf P. Steinbach

unread,
Nov 29, 2016, 3:21:16 AM11/29/16
to
As far as I can tell Rubin Safir and Popping Mad are the same person.

- Alf

0 new messages