注册 登录
编程论坛 C++教室

未定义的标识符,明明定义了啊。。

哒哒哒啦啦啦 发布于 2016-05-16 22:32, 8327 次点击

头文件
*****************************************************************8
#ifndef TOUTOU_H
#define TOUTOU_H

#include<iostream>
#include<string>
using namespace std;
using std::iostream;
using std::string;
using std::cout;
class Screen {
public:
    using pos = std::string::size_type;

    Screen() = default;
    Screen(pos ht, pos wd) :height(ht), width(wd), conents(ht*wd, ' ') {}
    Screen(pos ht, pos wd, char c) :height(ht), width(wd), conents(ht*wd, c) {}

    char get() const { return conents[cursor]; }                                         ***********   cursor未定义
    char get(pos r, pos s)  const { return conents[r*width + s]; }
    Screen &move(pos r, pos s);
    Screen &set(char c);
    Screen &set(pos r, pos s, char c);

    const Screen &display(std::ostream &os) const { do_display(os); return *this; }
    Screen &display(std::ostream &os) { do_display(os); return *this; }

private:
    void do_display const (ostream &os) {
        os << conents;
    }
private:
    pos cursor = 0;
    pos height = 0;
    std::string conents;
    pos width = 0;
};
inline Screen& Screen::move(pos r, pos s) {
    cursor = r*width + s;
    return *this;
}

inline Screen& Screen::set(char c) {
    conents[cursor] = c;
    return *this;
}

inline Screen& Screen::set(pos r, pos s, char c) {
    conents[r*width + s] = c;
    return *this;
}
#endif
 
程序
**********************************************************************
#include"C:\Users\Jin\Desktop\toutou.h"
int main()
{
   

   
    Screen myScreen(5,5,'X');                            ******************* Screen未定义
    myScreen.move(4, 0).set('#').display(cout);          ******************* cout未定义
     cout << "\n";
    myScreen.display(cout);
    cout << "\n";

    return 0;
}
3 回复
#2
rjsp2016-05-17 08:47
代码抄的吧,而且还是抄了一个对C++狗屁不通的人的代码。各种C++规范不遵守,风格和用法不遵守

程序代码:
#include <iostream>
#include <string>

class Screen
{
public:
    using pos = std::string::size_type;

    Screen() = default;
    Screen( pos height, pos width, char c ) :height_(height), width_(width), content_(height_*width_,c)
    {
    }

    Screen& move( pos r, pos s )
    {
        cursor_ = r*width_ + s;
        return *this;
    }

    char at() const
    {
        return content_[cursor_];
    }
    char& at()
    {
        return content_[cursor_];
    }

    char at( pos r, pos c )  const
    {
        return content_[r*width_+c];
    }
    char& at( pos r, pos c )
    {
        return content_[r*width_+c];
    }

private:
    pos height_ = 0;
    pos width_ = 0;
    std::string content_;
    pos cursor_ = 0;

    friend std::ostream& operator<<( std::ostream& os, const Screen& scr );
};

std::ostream& operator<<( std::ostream& os, const Screen& scr )
{
    for( auto r=0; r!=scr.height_; ++r )
    {
        os.write( scr.content_.c_str()+r*scr.width_, scr.width_ );
        os.write( "\n", 1 );
    }
    return os;
}

using namespace std;

int main( void )
{
    Screen scr( 5, 5, 'X' );
    cout << scr << '\n';

    scr.move(4,0).at() = '#';
    cout << scr << '\n';

    scr.at(4,4) = '#';
    cout << scr << '\n';

    return 0;
}

输出
XXXXX
XXXXX
XXXXX
XXXXX
XXXXX

XXXXX
XXXXX
XXXXX
XXXXX
#XXXX

XXXXX
XXXXX
XXXXX
XXXXX
#XXX#

#3
哒哒哒啦啦啦2016-05-17 20:36
回复 2楼 rjsp
是一个题在github上的答案,不知原来的代码为啥不对呢
#4
QQ3286269692016-05-18 22:09
1