读书人

初学者请问下类的问题

发布时间: 2013-09-28 10:01:20 作者: rapoo

菜鸟请教下类的有关问题
#include<iostream>
#include<string>
using namespace std;
class Screen {
public:
void home(){ _cursor = 0; }
char get() { return _screen[_cursor]; }
void get(int,int);
void move( int, int );
int height() { return _height; };
int width() { return _width; };
void set( const string &s );
void set( char ch );
void copy( const Screen &sobj );
void checkRange( int , int );
// ...
private:
string _screen;
string::size_type _cursor;
short _height, _width;
inline int remainingSpace();
};
void Screen::set( const string &s )//
{ // 在当前 _cursor 位置写字符串
int space = remainingSpace();
int len = s.size();
if ( space < len ) {
cerr << "Screen: warning: truncation: "
<< "space: " << space
<< "string length: " << len << endl;
len = space;
}
_screen.replace( _cursor, len, s );
_cursor += len - 1;
}


void Screen::set( char ch )//
{
if ( ch == '\0' )
cerr << "Screen: warning: "
<< "null character (ignored).\n";
else _screen[_cursor] = ch;
}
inline int Screen::remainingSpace()
{ // 当前位置不再是剩余的
int sz = _width * _height;
return( sz - _cursor );
}


#include "Screen.cpp"
#include <iostream>
using namespace std;
int main() {
Screen sobj(3,3); // 13.3.4 节定义的构造函数
string init("abcdefghi");
cout << "Screen Object ("
<< sobj.height() << ", "
<< sobj.width() << " )\n\n";
// 设置屏幕的内容
string::size_type initpos = 0;
int ix=1,iy=1;
for ( ix = 1; ix <= sobj.width(); ++ix )
for ( iy = 1; iy <= sobj.height(); ++iy )
{
sobj.move( ix, iy );
sobj.set( init[ initpos++ ] );
}
// 打印屏幕的内容
for ( ix = 1; ix <= sobj.width(); ++ix )
{
for ( iy = 1; iy <= sobj.height(); ++iy )
cout<<sobj.get();
cout << "\n";
}
return 0;
}

编译总是这里出现错误error C2661: 'Screen::Screen' : no overloaded function takes 2 parameters
Error executing cl.exe. 类 error?C2661
[解决办法]
你类里面没有这种Screen(int,int)构造函数啊
[解决办法]
你的类缺少这种类型的构造函数,你的注释也写了是13.3.4 节定义的构造函数,翻翻那里看看
[解决办法]
takes 2 parameters---你确定13.3.4 节定义的构造函数是两个参数的

读书人网 >C++

热点推荐