出现错误:
抄的C++primer的代码,删去第二个重载的get函数的显式声明的inline后能正常运行,不删显式LINK2019错误:
Screen.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Screen {
typedef string::size_type pos;//using ...=... also ok!
private:
pos height{}, width{}, cursor{};
string contents{};
mutable size_t access_ctr{};//最好显示的初试化他,否则有的编译器不识别
public:
Screen() = default;
Screen(pos ht, pos wd, char c) :height{ ht }, width{ wd }, contents(ht*wd, c){
}
char get() const {
return contents[cursor];
}//隐式内联
//下一句显式内联出现了错误
inline char get(pos r, pos c) const;
Screen &move(pos r, pos c);
void some_member() const;
size_t get_access_ctr() const {
return access_ctr;
}
};
Screen.cpp
#include<iostream>
#include"Screen.h"
using namespace std;
Screen &Screen::move(pos r, pos c) {
pos row = r * width;
cursor = row + c;
return *this;
}
char Screen::get(pos r,pos c) const{
pos row = r*width;
return contents[row + c];
}
void Screen::some_member() const {
access_ctr++;
}
main.cpp
#include<iostream>
#include"Screen.h"
using namespace std;
#include<string>
void main() {
typedef string::size_type pos;
Screen my_screen;
char ch=my_screen.get();
ch = my_screen.get(static_cast<pos>(0),static_cast<pos>(0) );
my_screen.some_member();
cout << my_screen.get_access_ctr() << endl;
system("pause");
}