Reputation: 16
I have these two examples from c++ primer, trying to declare a class member function outside the class definition. The first one gives me an error even when I remove the friendship and definition. The second one works fine. Any hints?
Error:
src/Screen.h:16:47: error: no ‘void Window_mgr::clear(Window_mgr::ScreenIndex)’ member function declared in class ‘Window_mgr’
Ex1:
#ifndef SCREEN_H
#define SCREEN_H
#include <string>
#include <vector>
class Screen;
class Window_mgr {
public:
using ScreenIndex = std::vector<Screen>::size_type;
Window_mgr();
private:
std::vector<Screen> screens;
};
void Window_mgr::clear(Window_mgr::ScreenIndex);
class Screen {
//friend void Window_mgr::clear(ScreenIndex);
public:
using pos = std::string::size_type;
Screen() = default;
Screen(pos h, pos w): height(h), width(w), contents(h*w, ' ') { }
Screen(pos h, pos w, char c): height(h), width(w), contents(h*w, c) { }
char get() const { return contents[cursor]; }
inline char get(pos, pos) const;
Screen &move(pos, pos);
Screen &set(char c) { contents[cursor] = c; return *this; }
Screen &set(pos, pos, char);
const Screen &display(std::ostream &os) const { do_display(os); return *this; }
Screen &display(std::ostream &os) { do_display(os); return *this; }
pos size() const;
private:
const void do_display(std::ostream &os) const { os << contents; }
pos cursor = 0;
pos height = 0, width = 0;
std::string contents;
};
inline
Window_mgr::Window_mgr(): screens{Screen(24, 80, ' ')} { }
char Screen::get(pos r, pos c) const
{ pos row = r * width; return contents[row + c]; }
inline Screen& Screen::move(pos r, pos c)
{ pos row = r * width; cursor = row + c; return *this; }
inline Screen& Screen::set(pos r, pos c, char ch)
{ pos row = r * width; contents[row + c] = ch; return *this; }
//inline void Window_mgr::clear(ScreenIndex i)
//{ Screen &s = screens[i]; s.contents = std::string(s.height * s.width, ' '); }
inline Screen::pos Screen::size() const
{ return height * width; }
#endif
Ex2:
#include <iostream>
int height;
class Screen {
public:
typedef std::string::size_type pos;
void set_height(pos);
pos height = 0;
};
Screen::pos verify(Screen::pos);
//void Screen::set_height(pos var) { height = verify(var); }
//Screen::pos verify(Screen::pos a) { return a; }
int main(){
return 0;
}
Upvotes: 0
Views: 8886
Reputation: 385098
No hints available.
You simply cannot do that.
The class definition must be a full picture of the members contained within that class.
Upvotes: 0
Reputation: 928
You cannot declare member outside its class. You can define a member function outside of the class, if you have declared it inside. Second example simply defines a global function verify, which uses the public types from the class Screen, but itself is not a member of Screen.
Upvotes: 10