-
October 16th, 2014, 12:50 PM
#1
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.
-
October 16th, 2014, 12:53 PM
#2
Re: What is wrong with my code?
Perhaps you wanted to declare one of the two operator[] overloads to be a const member function?
-
October 16th, 2014, 01:34 PM
#3
Re: What is wrong with my code?
Originally Posted by laserlight
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.
-
October 16th, 2014, 03:03 PM
#4
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)
-
October 16th, 2014, 03:17 PM
#5
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.
-
October 16th, 2014, 05:52 PM
#6
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|