better_call_Soul
2018-10-10 03:05
采纳率: 33.3%
浏览 782

C++将类成员函数显式内联为何出现LINK2019错误?

出现错误:
图片说明

 抄的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");
}   
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

3条回答 默认 最新

相关推荐 更多相似问题