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

non static member warning

15 views
Skip to first unread message

Popping mad

unread,
Nov 29, 2016, 12:43:36 AM11/29/16
to
I was making this mock code up to isolate a problem and it suprised me with this new compiler error that I really don't understand in the context of my program

#include <iostream>
#include <vector>

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)
{
for(int i = 0; i < _ofthings.size(); i++ ){
std::cout << i << "\t";
}
return os;
};

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

private:

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

};


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

std::vector<int> test {0};
for(int i = 0; i<10; i++ ){
test[i] = i;
std::cout << test[i] << "\t";
}
std::cout << std::endl;

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






return EXIT_SUCCESS;
} /* ---------- end of function main ---------- */

It returns:
|| g++ -Wall -ggdb -pg -M *.c >make.deps
|| g++ -Wall -ggdb -pg -pg -pthread -o lvalue.exe lvalue.c
|| lvalue.c: In function ‘std::ostream& vect::operator<<(std::ostream&, vect::Lvalue<T>)’:
lvalue.c|61 col 25| error: invalid use of non-static data member ‘vect::Lvalue<T>::_ofthings’
|| for(int i = 0; i < _ofthings.size(); i++ ){
|| ^~~~~~~~~
lvalue.c|69 col 20| note: declared here
|| std::vector<T> _ofthings;
|| ^~~~~~~~~


I'm not making any const variable or a static variable. The cases I see for this in an only search involve classes wrapped in classes.

Alf P. Steinbach

unread,
Nov 29, 2016, 12:58:48 AM11/29/16
to
On 29.11.2016 06:43, Popping mad wrote:
>
> It returns:
> || g++ -Wall -ggdb -pg -M *.c >make.deps
> || g++ -Wall -ggdb -pg -pg -pthread -o lvalue.exe lvalue.c
> || lvalue.c: In function ‘std::ostream& vect::operator<<(std::ostream&, vect::Lvalue<T>)’:
> lvalue.c|61 col 25| error: invalid use of non-static data member ‘vect::Lvalue<T>::_ofthings’
> || for(int i = 0; i < _ofthings.size(); i++ ){
> || ^~~~~~~~~
> lvalue.c|69 col 20| note: declared here
> || std::vector<T> _ofthings;
> || ^~~~~~~~~
>
>
> I'm not making any const variable or a static variable.

The `friend` function is not a member function.

There is no `this` object.


Cheers & hth.,

- Alf

ruben safir

unread,
Nov 29, 2016, 2:52:29 AM11/29/16
to
On 11/29/2016 12:55 AM, Alf P. Steinbach wrote:
>> lvalue.c|69 col 20| note: declared here
>> || std::vector<T> _ofthings;
>> || ^~~~~~~~~
>>
>>
>> I'm not making any const variable or a static variable.
>
> The `friend` function is not a member function.
>
> There is no `this` object.


yeah, I take it back. You can be right sometimes. I fixed that but it
came back with a different error. I'll post it later. This is supposed
to be the simplified test case of a larger program. And it is tripping
me up on many parts.


0 new messages