I have this class (full code) in Screen.h:
#include <string>
#include <iostream>
class Screen {
using pos = std::string::size_type;
private:
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
public:
Screen() = default; // needed because Screen has another constructor
Screen(pos ht, pos wd) : Screen(ht, wd, ' ') {}
Screen(pos ht, pos wd, char c): height(ht), width(wd),
contents(ht * wd, c) { }
char get() const {
return contents[cursor];
}
Screen& move(pos r, pos c);
inline char get(pos ht, pos wd) const; // explicitly inline
Screen& set(char);
const Screen& display(std::ostream&) const;
Screen& display(std::ostream&);
private:
void doDisplay(std::ostream&) const;
};
and the implementation for it in Screen.cpp:
#include "Screen.h"
#include <iostream>
char Screen::get(pos r, pos c) const // declared as inline in the class
{
pos row = r * width; // compute row location
return contents[row + c]; // return character at the given column
}
inline Screen& Screen::move(pos r, pos c) {
pos row = r * width;
cursor = row + c;
return *this;
}
Screen& Screen::set(char c) {
contents[cursor] = c;
return *this;
}
Screen& Screen::display(std::ostream& outputStream) {
doDisplay(outputStream);
return *this;
}
const Screen& Screen::display(std::ostream& outputStream) const {
doDisplay(outputStream);
return *this;
}
void Screen::doDisplay(std::ostream& outputStream) const {
for (unsigned h = 0; h < height; ++h) {
for (unsigned w = 0; w < width; ++w) {
outputStream << contents[h*w];
}
outputStream << std::endl;
}
}
My main file:
#include "Screen.h"
#include <iostream>
int main() {
Screen myScreen(5, 5, 'X');
myScreen.move(0,4).set('#').display(std::cout);
std::cout << std::endl;
int returnCode = EXIT_SUCCESS;
return returnCode;
}
this produces the following linker error: undefined reference to `Screen::move(unsigned long long, unsigned long long)'
When I remove the "inline" from Screen::move, it works, but somehow the char at 0,4 doesn't get changed... I don't really know what's not working. I'm using Code::Blocks with the gcc compiler.
Edit: okay everything works fine when I remove the "inline" from the definition of the "move" method. But now my question is: why can't I specify "inline" in Screen.cpp?