CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jul 2005
    Posts
    1,030

    What is wrong with my code?

    Here is the code,
    Code:
    template<class T>
    class Array1D
    {
    public:
    	Array1D(int x):_size(x)
    	{
    		data = new T[_size];
    	}
    
    	~Array1D()
    	{
    		delete[] data;
    	}
    
    	T operator[](int x)
    	{
    		return data[x];
    	}
    
    	T& operator[](int x)
    	{
    		return data[x];
    	}
    private:
    	int _size;
    	T* data;
    };
    
    template<class T>
    class Array2D
    {
    public:
    	Array2D(int row, int col):_row(row), _col(col)
    	{
    		pArr = new Array1D*[_row];
    
    		for(int i=0;i<_row;++i)
    			pArr[i] = new Array1D[_col];
    	}
    
    	~Array2D()
    	{
    		for(int i=0;i<_row;++i)
    			delete[] pArr[i];
    
    		delete[] pArr;
    	}
    
    	Array1D operator[](int x)
    	{
    		return *pArr[x];
    	}
    
    	Array1D& operator[](int x)
    	{
    		return *pArr[x];
    	}
    private:
    	Array1D** pArr;
    	int _row;
    	int _col;
    };
    
    int main(){
    	Array2D<int> arr(3, 4);
    	arr[1][2] = 2;
     	return 0;
    }
    Basically, there is a compiler error "error C2556: 'Array1D &Array2D<T>:perator [](int)' : overloaded function differs only by return type from 'Array1D Array2D<T>:perator [](int)'". What does it mean? Thanks.

  2. #2
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: What is wrong with my code?

    Perhaps you wanted to declare one of the two operator[] overloads to be a const member function?
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

  3. #3
    Join Date
    Jul 2005
    Posts
    1,030

    Re: What is wrong with my code?

    Quote Originally Posted by laserlight View Post
    Perhaps you wanted to declare one of the two operator[] overloads to be a const member function?
    Yes! That works. But this mistake is also related to the compiler error "error C2040: 'Array2D<T>:perator []' : 'Array1D<T> &(int)' differs in levels of indirection from 'Array1D<T> (int)'". How is this error related to the overloaded operator[]? Thanks.

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,824

    Re: What is wrong with my code?

    Code:
    pArr = new Array1D*[_row];
    
    		for(int i=0;i<_row;++i)
    			pArr[i] = new Array1D[_col];
    You haven't specified the type to use with Array1D which takes a template argument of the type to use to instantiate the Array1D class. Also note that using the array version of new calls the default constructor for the type specified which you haven't included for Array1D.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    Jun 2009
    Location
    France
    Posts
    2,513

    Re: What is wrong with my code?

    You *just* defined the Array1D class. Why aren't you using it in your Array2D class? Array1D<Array1D<T>*>
    Is your question related to IO?
    Read this C++ FAQ article at parashift by Marshall Cline. In particular points 1-6.
    It will explain how to correctly deal with IO, how to validate input, and why you shouldn't count on "while(!in.eof())". And it always makes for excellent reading.

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: What is wrong with my code?

    1) Both classes absolutely need a Copy Constructor and and copy assignment operator

    2) Get Array1D debugged before trying to use it in Array2D

    3) Why does Array2D have this data member:

    Code:
    Array1D** pArr;
    It should be either:

    Code:
    T ** pArr;           // in which case, you would not use Array1D ... or
    Array1D<T> * pArr;  // then you would use the Array1D class
    4) There is probably more ...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured