C++无法在主函数中调用fen

我用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;
}

 

请写出错误信息

因为你使用了inline关键字,内联函数的定义必须出现在第一次调用之前。

一个inline函数会在多个源文件中被用到,那么必须把它定义在头文件中

您好,我是有问必答小助手,您的问题已经有小伙伴解答了,您看下是否解决,可以追评进行沟通哦~

如果有您比较满意的答案 / 帮您提供解决思路的答案,可以点击【采纳】按钮,给回答的小伙伴一些鼓励哦~~

ps:问答VIP仅需29元,即可享受5次/月 有问必答服务,了解详情>>>https://vip.csdn.net/askvip?utm_source=1146287632

也许对你有帮助:https://blog.csdn.net/it_xiangqiang/category_10581430.html

非常感谢您使用有问必答服务,为了后续更快速的帮您解决问题,现诚邀您参与有问必答体验反馈。您的建议将会运用到我们的产品优化中,希望能得到您的支持与协助!

速戳参与调研>>>https://t.csdnimg.cn/Kf0y