Error: invalid use of member (did you forget the ‘&’ ?)

This is a discussion on Error: invalid use of member (did you forget the ‘&’ ?) within the C++ Programming forums, part of the General Programming Boards category; Good night, The program I'm developing does compile correctly, but when I perform some tests it crashes... I've searched for ...

  1. #1
    Registered User
    Join Date
    May 2008
    Posts
    20

    Exclamation Error: invalid use of member (did you forget the ‘&’ ?)

    Good night,

    The program I'm developing does compile correctly, but when I perform some tests it crashes... I've searched for this error in a dozen pages but I couldn't find the answer to my problems :|

    So the code is this (it's a little bit extensive but I think it's better this way to give all the info possible to debug it):

    Code:
    #include "cstring.cpp"
    #include<cassert>
    #include<iostream>
    
    using namespace std;
    
    template<class T> class Vector;
    
    template<class T>
    ostream& operator<<(ostream&, const Vector<T>&);
    
    template <class T>
    class Vector
    {
          friend ostream& operator<< <>(ostream&, const Vector<T>&);
    private:
          T* data;
          unsigned len;
    public:
          Vector(unsigned = 10);
          Vector(const Vector<T>&);
          virtual ~Vector(void);
          Vector<T>& operator =(const Vector<T>&);
          bool operator==(const Vector<T>&);
          T& operator [](unsigned);
    	  unsigned getLength(void) {return len;}
    	  T* getData(void) {return data;}
    };
    
    template <class T>
    ostream& operator<<(ostream& os, const Vector<T>& v)
    {
    	unsigned i;
    
    	for(i=0; i<v.len; i++)
    		os << v.data[i];
    
    	return os;
    }
    
    template <class T>
    Vector<T>::Vector(unsigned size)
    {
    	len=size;
    	data = new T[len];
    }
    
    template <class T>
    Vector<T>::Vector(const Vector<T>& v)
    {
    	unsigned i;
    
    	len = v.len;
    	data = new T[len];
    
    	for(i=0; i<len; i++)
    		data[i]=v.data[i];
    }
    
    template <class T>
    Vector<T>::~Vector()
    {
    	cout << "Deleting data..." << endl;
    
    	delete [] data;
    }
    
    template <class T>
    Vector<T>& Vector<T>::operator=(const Vector<T>& v)
    {
    	unsigned i;
    
    	len = v.len;
    	delete[] data;
    
    	data = new T[len];
    
    	for(i=0; i<len; i++)
    		data[i]=v.data[i];
    
    	return *this;
    }
    
    template <class T>
    bool Vector<T>::operator==(const Vector<T>& v)
    {
    	unsigned i;
    
    	if(len!=v.len)
    		return false;
    	else
    	{
    		for(i=0; i<len; i++)
    		{
    			if(data[i]!=v.data[i])
    				return false;
    		}
    
    		return true;
    	}
    }
    
    template <class T>
    T& Vector<T>::operator [](unsigned n)
    {
    	unsigned i;
    
    	assert(n>=0);
    
    	if(n>=len) // vector dinâmico
    	{
    		T* newData = new T[n+1];
    
    		for(i=0; i<len; i++)
    			newData[i]=data[i];
    
    		delete [] data;
    
    		data = newData;
    		len=n+1;
    	}
    
    	return data[n];
    }
    
    template<class P, class Q>
    class Pair
    {
          P p;
          Q q;
    public:
          Pair(const P& _p = P(), const Q& _q = Q()): p(_p), q(_q) {}
          P& objectP() {return p;}
          Q& objectQ() {return q;}
    };
    
    // herança
    template<class KeyType, class ValueType>
    class AssociativeArrayInheritance : public Vector<Pair<KeyType, ValueType> >
    {
    public:
    	AssociativeArrayInheritance(unsigned size=0):Vector<Pair<KeyType, ValueType> >(size){}
    	ValueType& operator [](const KeyType);
    };
    
    template<class KeyType, class ValueType>
    ValueType& AssociativeArrayInheritance<KeyType, ValueType>::operator [](const KeyType key)
    {
    	unsigned  i;
    	Pair<KeyType, ValueType> pair(key);
    
    	for(i=0; i<Vector<Pair<KeyType,ValueType> >::getLength(); i++)
    	{
    		pair = Vector<Pair<KeyType,ValueType> >::getData[i];
    		if(pair.objectP() == key)
    			return pair.objectQ();
    	}
    
    	Vector<Pair<KeyType,ValueType> >::getData[Vector<Pair<KeyType,ValueType> >::getLength()+1]=pair;
    
    	return Vector<Pair<KeyType,ValueType> >::getData[Vector<Pair<KeyType,ValueType> >::getLength()-1].objectQ();
    }
    
    // composição
    template <class KeyType, class ValueType>
    class AssociativeArrayComposition
    {
    private:
    	Vector<Pair<KeyType, ValueType> > pairs;
    	unsigned len;
    public:
    	AssociativeArrayComposition(unsigned size=0):pairs(size)
    	{
    		len = size;
    	}
    	virtual ~AssociativeArrayComposition(void);
    	ValueType& operator [](const KeyType);
    };
    
    template <class KeyType, class ValueType>
    AssociativeArrayComposition <KeyType, ValueType>::~AssociativeArrayComposition()
    {
    	delete [] &pairs;
    }
    
    template<class KeyType, class ValueType>
    ValueType& AssociativeArrayComposition<KeyType, ValueType>::operator [](const KeyType key)
    {
    	Pair<KeyType, ValueType> pair(key);
    
    	for(unsigned i=0; i < len; i++)
    	{
    		pair = pairs[i];
    		if(pair.objectP() == key)
    			return pair.objectQ();
    	}
    
    	pairs[len]=pair;
    	len++;
    	return pairs[len-1].objectQ();
    }
    The errors occur in the lines that are bold (in class 'AssociativeArrayInheritance').

    The tests I made that originate these errors is:

    AssociativeArrayInheritance<String, int> table;
    String key1 (3, 'a'); // creates "aaa" string
    table[key1] = 15; // error is here
    Again I apologize for the lengthy code but I think is a necessary mean :x

    Anybody has an idea? I think I have posted all the info needed, if not please say to me.

    Thanks in advance,
    João

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    If it compiles correctly, where did the title come from? Anyway, looking at the bold,
    Code:
    pair = Vector<Pair<KeyType,ValueType> >::getData[i];
    wouldn't you need () after getData? Edit: Or do you mean to call operator[]?

  3. #3
    Registered User
    Join Date
    May 2008
    Posts
    20

    Smile

    The errors occur when compiling a "teste.cpp" file with the tests I posted.

    I forgot to put the errors... sorry for that!

    Here it goes:

    vector.h: In member function ‘ValueType& AssociativeArrayInheritance<KeyType, ValueType>:perator[](KeyType) [with KeyType = String, ValueType = int]’:
    testes.cpp:92: instantiated from here
    vector.h:161: error: invalid use of member (did you forget the ‘&’ ?)
    testes.cpp:92: instantiated from here
    vector.h:166: error: invalid use of member (did you forget the ‘&’ ?)
    vector.h:168: error: invalid use of member (did you forget the ‘&’ ?)
    tabstop I'm attempting to call operator [], yes!

    []'s

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Quote Originally Posted by blacknail View Post


    tabstop I'm attempting to call operator [], yes!

    []'s
    Then do so. At the worst, it would seem to be
    Code:
    pair = Vector<Pair<KeyType,ValueType> >::operator[](i)
    I don't think you need to call getData first -- that gives you a T*, but operator [] works on a Vector. Or is the T* you get from getData the thing you want to use operator [] on? In that case, it would seem to be
    Code:
    pair = (Vector<Pair<KeyType,ValueType> >::getData())[i]
    One pair of parentheses to make sure getData is a function call, the other set because I'm paranoid.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    20

    Wink

    tabstop it's the first option yes, thank you, I will try that!

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    55
    "Invalid use of member" and a file called "teste(s)"?
    Leave that member alone!

  7. #7
    The larch
    Join Date
    May 2006
    Posts
    3,573
    The whole thing doesn't work anyway. There is no place AssociativeArrayInheritance allocates any memory, it doesn't use the Vector class it is derived from (other than gaining access to its raw internal buffer, and asking for size which doesn't change anywhere), and the general logic of operator [] is broken (look what you are doing to pair!)

    In the "using composition" version the destructor invokes undefined behaviour (attempts to delete some random pointer - note, destructors would automatically call the destructors of members) and it's operator[] seems to suffer from some of the same problems, except it more properly uses the Vector's interface. Of course, since the Vector should be able to update it's length properly, there is no point in duplicating that data in the user class.

    Oh, and why on earth are you including a "cstring.cpp"?!
    Last edited by anon; 11-06-2008 at 11:04 AM.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. New to C++ Classes
    By Cypher in forum C++ Programming
    Replies: 122
    Last Post: 07-05-2007, 01:49 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Menu Item Caption - /a for right aligned Accelerator?
    By JasonD in forum Windows Programming
    Replies: 6
    Last Post: 06-25-2003, 11:14 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21