我用VS2019学习《C++ Primer》第七章的习题screen类的创建
我创建了一个类,将声明整体放在了头文件,但是类中方法的实现放在一个cpp文件中,我又另创建了一个cpp用来创建主函数,但是创建主函数后,在主函数中调用类的方法 就会报错,如果将类的函数定义放回头文件中就不会报错,请问是什么啊?
这是头文件
#pragma once
#ifndef screen_h_include
#define screen_h_include
#include<string>
using namespace std;
class screen
{
public:
//typedef string::size_type unsigned;
screen() = default;
screen(unsigned ht, unsigned wd, char c ) :height(ht), width(wd), contents(ht* wd, c) {}
screen(unsigned ht, unsigned wd) :height(ht), width(wd), contents(ht* wd, ' ') {}
void some_member() const;
char get() const{
return contents[cursor];
}
inline char get(unsigned ht, unsigned wd) const;
screen& move(unsigned r, unsigned c);
screen& set(char);
screen& set(unsigned, unsigned, char);
screen& display(ostream& os);
const screen& display(ostream& os) const;
private:
unsigned cursor = 0;
unsigned height = 0, width = 0;
string contents;
mutable size_t access_ctr;
void do_display(ostream& os) const{
os << contents;
}
};
这是类中函数的定义,放在另外一个cpp文件中
#include "screen.h"
inline
screen& screen::move(unsigned r, unsigned c) {
unsigned row = r * width;
cursor = row + c;
return *this;
}
char screen::get(unsigned r, unsigned c)const {
unsigned row = r * width;
return contents[row + c];
}
inline
void screen::some_member()const {
++access_ctr;
}
inline
screen& screen::set(char c) {
contents[cursor] = c; //设置当前光标所在位置的新值
return *this; //将this对象作为左值返回
}
inline
screen& screen::set(unsigned r, unsigned col, char ch) {
contents[r * width + col] = ch;//设计给定位置的新值
return *this;
}
inline
screen& screen::display(ostream& os) {
do_display(os);
return *this;
}
const screen& screen::display(ostream& os) const {
do_display(os);
return *this;
}
这是主函数:
#include<iostream>
#include<string>
#include"screen.h"
using namespace std;
int main() {
screen myScreen(5, 3,'X');
myScreen.move(4, 0).set('#').display(cout);
cout << "\n";
myScreen.display(cout);
cout << "\n";
}
可如果按照下面,把函数的定义放回头文件就没有任何问题,但依旧会对我的构造函数提出警示,说我没有初始化:
#pragma once
#ifndef screen_h_include
#define screen_h_include
#include<string>
using namespace std;
class screen
{
public:
//typedef string::size_type unsigned;
screen() = default;
screen(unsigned ht, unsigned wd, char c ) :height(ht), width(wd), contents(ht* wd, c) {}
screen(unsigned ht, unsigned wd) :height(ht), width(wd), contents(ht* wd, ' ') {}
void some_member() const;
char get() const{
return contents[cursor];
}
inline char get(unsigned ht, unsigned wd) const;
screen& move(unsigned r, unsigned c);
screen& set(char);
screen& set(unsigned, unsigned, char);
screen& display(ostream& os);
const screen& display(ostream& os) const;
private:
unsigned cursor = 0;
unsigned height = 0, width = 0;
string contents;
mutable size_t access_ctr;
void do_display(ostream& os) const{
os << contents;
}
};
inline
screen& screen::move(unsigned r, unsigned c) {
unsigned row = r * width;
cursor = row + c;
return *this;
}
char screen::get(unsigned r, unsigned c)const {
unsigned row = r * width;
return contents[row + c];
}
inline
void screen::some_member()const {
++access_ctr;
}
inline
screen& screen::set(char c) {
contents[cursor] = c; //设置当前光标所在位置的新值
return *this; //将this对象作为左值返回
}
inline
screen& screen::set(unsigned r, unsigned col, char ch) {
contents[r * width + col] = ch;//设计给定位置的新值
return *this;
}
inline
screen& screen::display(ostream& os) {
do_display(os);
return *this;
}
const screen& screen::display(ostream& os) const {
do_display(os);
return *this;
}