[][]重载问题第二弹

ghostzf 2006-07-03 07:44:58
按照more effective书上的原理写了以下的复合类想实现[][]运算符重载
经过改进,依然无法调试通过 ,搞了一天, 无果,只得再请高人继续指教

// 'array2d.h'

#ifndef ARRAY2D_H
#define ARRAY2D_H

#include <iostream.h>

template<class T>
class Array2D {
public:
class Array1D {
public:
Array1D(const Array2D&); // Translate 2D array into 1D array
~Array1D();
const T &operator[](int); // return the element
private:
T *Ptr;
};

Array2D(int = 10, int = 10);
~Array2D();
Array1D operator[](int); // return 1D array with correspondence row

private:
T *arrayPtr;
int rowSize;
int columnSize;
};

template<class T>
Array2D<T>::Array1D::Array1D(const Array2D<T> ©)
{
Ptr = new T[copy.columnSize];

// Assign the special row elements to 1D array

for (int i = 0; i < copy.columnSize; i++) {
*(Ptr+i) = *(copy.arrayPtr+i);
}
}

template<class T>
Array2D<T>::Array1D::~Array1D()
{
delete [] Ptr;
}

template<class T>
Array2D<T>::Array2D(int rSize, int cSize)
{
rowSize = rSize > 0 ? rSize : 10;
columnSize = cSize > 0 ? cSize : 10;
arrayPtr = new T[rowSize * columnSize];

for (int i = 0; i < rowSize * columnSize; i++) {
*(arrayPtr+i) = 0;
}
}

template<class T>
Array2D<T>::~Array2D()
{
delete [] arrayPtr;
}

template<class T>
Array2D<T>::Array1D Array2D<T>::operator[](int sub)
{
arrayPtr += sub * columnSize; // Let the arrayPtr point to the
// correspondence row
return *this;
}

template<class T>
const T& Array2D<T>::Array1D::operator[](int sub)
{
return *(Ptr+sub);
}

#endif

...全文
575 21 打赏 收藏 转发到动态 举报
写回复
用AI写文章
21 条回复
切换为时间正序
请发表友善的回复…
发表回复
triace_zhang 2006-07-03
  • 打赏
  • 举报
回复
楼主试试这个,我在vc6下通过了。

#include <iostream>
using namespace std;

template<class T>
class Array2D {
public:
class Array1D {
public:
Array1D(T* ptr) { Ptr = ptr; }
~Array1D(){};
T &operator[](int sub){ return *(Ptr+sub); }
private:
T *Ptr;
};

Array2D(int rSize = 10, int cSize = 10)
{
rowSize = rSize > 0 ? rSize : 10;
columnSize = cSize > 0 ? cSize : 10;
arrayPtr = new T[rowSize * columnSize];

for (int i = 0; i < rowSize * columnSize; i++) {
*(arrayPtr+i) = 0;
}
}
~Array2D(){ delete [] arrayPtr;}
Array1D operator[](int sub){ return arrayPtr + columnSize*sub; }

private:
T *arrayPtr;
int rowSize;
int columnSize;
};



int main()
{
Array2D<int> arr(3,3);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j] = i;
}
}

for(int k=0;k<3;k++)
{
for(int l=0;l<3;l++)
{
cout<<arr[k][l]<<" ";
}
}

}
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
我分别粘贴了,还是无法通过
// 'array2d.h'

#ifndef ARRAY2D_H
#define ARRAY2D_H

#include <iostream>
using namespace std;

template<class T>
class Array2D {
public:
class Array1D {
public:
Array1D(const Array2D<T> ©)
{
Ptr = new T[copy.columnSize];

// Assign the special row elements to 1D array

for (int i = 0; i < copy.columnSize; i++) {
*(Ptr+i) = *(copy.arrayPtr+i);
}
}
~Array1D()
{
delete [] Ptr;
}
const T& operator[](int sub)
{
return *(Ptr+sub);
}
private:
T *Ptr;
};

Array2D(int rSize = 10, int cSize = 10)
{
rowSize = rSize > 0 ? rSize : 10;
columnSize = cSize > 0 ? cSize : 10;
arrayPtr = new T[rowSize * columnSize];

for (int i = 0; i < rowSize * columnSize; i++) {
*(arrayPtr+i) = 0;
}
}
~Array2D()
{
delete [] arrayPtr;
};
Array1D operator[](int sub)
{
arrayPtr += sub * columnSize; // Let the arrayPtr point to the correspondence row

return *this;
}

private:
T *arrayPtr;
int rowSize;
int columnSize;
};

问题如下:
--------------------Configuration: test - Win32 Debug--------------------
Compiling...
test.cpp
e:\array2d.h(16) : error C2248: 'columnSize' : cannot access private member declared in class 'Array2D<int>'
e:\array2d.h(60) : see declaration of 'columnSize'
e:\array2d.h(14) : while compiling class-template member function '__thiscall Array2D<int>::Array1D::Array2D<int>::Array1D(const class Array2D<int> &)'
e:\array2d.h(20) : error C2248: 'columnSize' : cannot access private member declared in class 'Array2D<int>'
e:\array2d.h(60) : see declaration of 'columnSize'
e:\array2d.h(14) : while compiling class-template member function '__thiscall Array2D<int>::Array1D::Array2D<int>::Array1D(const class Array2D<int> &)'
e:\array2d.h(21) : error C2248: 'arrayPtr' : cannot access private member declared in class 'Array2D<int>'
e:\array2d.h(58) : see declaration of 'arrayPtr'
e:\array2d.h(14) : while compiling class-template member function '__thiscall Array2D<int>::Array1D::Array2D<int>::Array1D(const class Array2D<int> &)'
Error executing cl.exe.

test.obj - 3 error(s), 0 warning(s)
triace_zhang 2006-07-03
  • 打赏
  • 举报
回复
就是把本在在类体外定义的类成员函数放到类体中定义,比如:

class A{
public:
void f(); //只声明
};

void A::f() //在类体外定义
{
cout << "1";
}

改为
class A{
public:
void f() { cout << "1";} //在类体内定义
};

果然这样VC6的问题解决了。
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
我使用的是vc6,“就上要把代码放在里面”这话什么意思
lyskyly 2006-07-03
  • 打赏
  • 举报
回复
如果真是编译器的问题就帮不上忙了
Dong 2006-07-03
  • 打赏
  • 举报
回复
请问你使用VC6吗?
我遇到过这样的问题,就上要把代码放在里面,不然VC6的编译器认不了代码
#include <iostream>
using namespace std;

template<class T>
class Array2D {
public:
class Array1D {
public:
Array1D(T* ptr) { Ptr = ptr; }
~Array1D(){};
T &operator[](int sub){ return arrayPtr + columnSize*sub; }
private:
T *Ptr;
};

Array2D(int rSize = 10, int cSize = 10)
{
rowSize = rSize > 0 ? rSize : 10;
columnSize = cSize > 0 ? cSize : 10;
arrayPtr = new T[rowSize * columnSize];

for (int i = 0; i < rowSize * columnSize; i++) {
*(arrayPtr+i) = 0;
}
}
~Array2D(){ delete [] arrayPtr;}
Array1D operator[](int sub){ return *(Ptr+sub); }

private:
T *arrayPtr;
int rowSize;
int columnSize;
};


#include<iostream>
using namespace std;
int main()
{
Array2D<int> arr(3,3);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j] = i;
}
}

for(int k=0;k<3;k++)
{
for(int l=0;l<3;l++)
{
cout<<arr[k][l]<<" ";
}
}

}
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
晕,那总不能遇到这样的问题就回避吧,如何解决呢
triace_zhang 2006-07-03
  • 打赏
  • 举报
回复
LZ用的是VC6吧,模板的问题多多啊。用同样的代码后面的2003或者dev C++就没问题。
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
按照你的建议还是依旧。我的源代码就是你在上面写的啊,我把我站贴的所有完全内容放出来,你看看是不是跟你的一样。

#include <iostream>
using namespace std;

template<class T>
class Array2D {
public:
class Array1D {
public:
Array1D(T* ptr); // Translate 2D array into 1D array
~Array1D();
T &operator[](int); // return the element
private:
T *Ptr;
};

Array2D(int = 10, int = 10);
~Array2D();
Array1D operator[](int); // return 1D array with correspondence row

private:
T *arrayPtr;
int rowSize;
int columnSize;
};

template<class T>
Array2D<T>::Array1D::Array1D(T* ptr)
{
Ptr = ptr;
}

template<class T>
Array2D<T>::Array1D::~Array1D()
{
}

template<class T>
Array2D<T>::Array2D(int rSize, int cSize)
{
rowSize = rSize > 0 ? rSize : 10;
columnSize = cSize > 0 ? cSize : 10;
arrayPtr = new T[rowSize * columnSize];

for (int i = 0; i < rowSize * columnSize; i++) {
*(arrayPtr+i) = 0;
}
}

template<class T>
Array2D<T>::~Array2D()
{
delete [] arrayPtr;
}

template<class T>
typename Array2D<T>::Array1D Array2D<T>::operator[](int sub)
{
// Let the arrayPtr point to the
// correspondence row
return arrayPtr + columnSize*sub;
}

template<class T>
T& Array2D<T>::Array1D::operator[](int sub)
{
return *(Ptr+sub);
}


#include<iostream>
using namespace std;
int main()
{
Array2D<int> arr(3,3);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j] = i;
}
}

for(int k=0;k<3;k++)
{
for(int l=0;l<3;l++)
{
cout<<arr[k][l]<<" ";
}
}

}
lyskyly 2006-07-03
  • 打赏
  • 举报
回复
那就奇怪了,你显示的错误全时链接错误,
楼主重新建一个常规控制台程序试一试把,不要建Win32程序
楼主把你的源码全部贴出来看看
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
我就是新生成一个cpp文件,然后把你的代码全部粘贴进去,(把endif删除了),但是问题就是如上
lyskyly 2006-07-03
  • 打赏
  • 举报
回复
楼主把你测试时的文件包含关系也贴出来,要么重新建一个工程试一试
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
奇怪了,问题还是如下:
--------------------Configuration: temp - Win32 Debug--------------------
Linking...
temp.obj : error LNK2001: unresolved external symbol "public: __thiscall Array2D<int>::Array1D::~Array1D(void)" (??1Array1D@?$Array2D@H@@QAE@XZ)
temp.obj : error LNK2001: unresolved external symbol "public: int & __thiscall Array2D<int>::Array1D::operator[](int)" (??AArray1D@?$Array2D@H@@QAEAAHH@Z)
temp.obj : error LNK2001: unresolved external symbol "public: __thiscall Array2D<int>::Array1D::Array1D(int *)" (??0Array1D@?$Array2D@H@@QAE@PAH@Z)
Debug/temp.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.
lyskyly 2006-07-03
  • 打赏
  • 举报
回复
在Dev C++ 4.9.9.2中也能编译通过
lyskyly 2006-07-03
  • 打赏
  • 举报
回复
我是在VS2005中编译的,楼主报的什么错
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
浮生三笑,你的程序我还是无法调试通过阿
lyskyly 2006-07-03
  • 打赏
  • 举报
回复
我的代码上面多了个#endif
楼主请删了,
改了一些认为不合理的地方,可能不符合楼主的原意
lyskyly 2006-07-03
  • 打赏
  • 举报
回复
#include <iostream>
using namespace std;

template<class T>
class Array2D {
public:
class Array1D {
public:
Array1D(T* ptr); // Translate 2D array into 1D array
~Array1D();
T &operator[](int); // return the element
private:
T *Ptr;
};

Array2D(int = 10, int = 10);
~Array2D();
Array1D operator[](int); // return 1D array with correspondence row

private:
T *arrayPtr;
int rowSize;
int columnSize;
};

template<class T>
Array2D<T>::Array1D::Array1D(T* ptr)
{
Ptr = ptr;
}

template<class T>
Array2D<T>::Array1D::~Array1D()
{
}

template<class T>
Array2D<T>::Array2D(int rSize, int cSize)
{
rowSize = rSize > 0 ? rSize : 10;
columnSize = cSize > 0 ? cSize : 10;
arrayPtr = new T[rowSize * columnSize];

for (int i = 0; i < rowSize * columnSize; i++) {
*(arrayPtr+i) = 0;
}
}

template<class T>
Array2D<T>::~Array2D()
{
delete [] arrayPtr;
}

template<class T>
typename Array2D<T>::Array1D Array2D<T>::operator[](int sub)
{
// Let the arrayPtr point to the
// correspondence row
return arrayPtr + columnSize*sub;
}

template<class T>
T& Array2D<T>::Array1D::operator[](int sub)
{
return *(Ptr+sub);
}

#endif

#include<iostream>
using namespace std;
int main()
{
Array2D<int> arr(3,3);
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
arr[i][j] = i;
}
}

for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<arr[i][j]<<" ";
}
}

}
ghostzf 2006-07-03
  • 打赏
  • 举报
回复
我的test.cpp是这样的
#include "array2d.h"

int main()
{
Array2D<int> a(2, 3);

cout << a[1][2] << endl;

return 0;
}


编译的错误是:
--------------------Configuration: test - Win32 Debug--------------------
Linking...
test.obj : error LNK2001: unresolved external symbol "public: __thiscall Array2D<int>::Array1D::~Array1D(void)" (??1Array1D@?$Array2D@H@@QAE@XZ)
test.obj : error LNK2001: unresolved external symbol "public: int const & __thiscall Array2D<int>::Array1D::operator[](int)" (??AArray1D@?$Array2D@H@@QAEABHH@Z)
test.obj : error LNK2001: unresolved external symbol "public: __thiscall Array2D<int>::Array1D::Array1D(class Array2D<int> const &)" (??0Array1D@?$Array2D@H@@QAE@ABV1@@Z)
Debug/test.exe : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

test.exe - 4 error(s), 0 warning(s)
triace_zhang 2006-07-03
  • 打赏
  • 举报
回复
只看了编译,没看实际效果。
这程序在VC6下可以编译通过,在dev c++上因为模板嵌套成员构成时的缺陷,导致Array2D<T>::Array1D 没有被隐式认为是个typename,所以显示加一下

template<class T>
Array2D<T>::Array1D Array2D<T>::operator[](int sub)
//上面改为 typename Array2D<T>::Array1D Array2D<T>::operator[](int sub)
{
arrayPtr += sub * columnSize;// Let the arrayPtr point to the
// correspondence row
return *this;
}
用class也行
加载更多回复(1)

64,642

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧