Reaper_Zero 2021-05-04 21:33 采纳率: 0%
浏览 27

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;
}
  • 写回答

5条回答 默认 最新

  • SoftwareTeacher 《编程之美》作者 2021-05-04 21:53
    关注

    请写出错误信息

    评论

报告相同问题?

悬赏问题

  • ¥15 webstorm上开发的vue3+vite5+typeScript打包时报错
  • ¥15 vue使用gojs,需求在link中的虚线上添加方向箭头
  • ¥15 CSS通配符清除内外边距为什么可以覆盖默认样式?
  • ¥15 SPSS分类模型实训题步骤
  • ¥15 求解决扩散模型代码问题
  • ¥15 工创大赛太阳能电动车项目零基础要学什么
  • ¥20 limma多组间分析最终p值只有一个
  • ¥15 nopCommerce开发问题
  • ¥15 torch.multiprocessing.spawn.ProcessExitedException: process 1 terminated with signal SIGKILL
  • ¥15 QuartusⅡ15.0编译项目后,output_files中的.jdi、.sld、.sof不更新怎么解决