在线求助,自己定义了类成员链接时却提示无法找到

贰到不行 2014-09-16 06:14:56
#include <iostream>

class screen
{
public:
screen &up()
{
std::cout << "up" << std::endl;
return *this;
}
screen &down()
{
std::cout << "down" << std::endl;
return *this;
}
screen &left()
{
std::cout << "left" << std::endl;
return *this;
}
screen &right()
{
std::cout << "right" << std::endl;
return *this;
}
enum direction{
UP, DOWN, LEFT, RIGHT, COUNT
};
public:
typedef screen &(screen::*action)();
static action Menu[screen::COUNT];/* =
{
&screen::up, &screen::down, &screen::left, &screen::right
};*/

};

int main()
{
screen::Menu[screen::UP] = &screen::up;
screen::Menu[screen::DOWN] = &screen::down;
screen::Menu[screen::LEFT] = &screen::left;
screen::Menu[screen::RIGHT] = &screen::right;
screen scr;
for(int i=0; i<screen::COUNT; i++)
{
(scr.*(screen::Menu[i]))();
}
return 0;
}
...全文
80 3 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
贰到不行 2014-09-16
  • 打赏
  • 举报
回复
已经解决了,跟static的作用有关: 即静态数据成员必须在类外进行有关的定义和初始化,在类体中只是完成了声明。
贰到不行 2014-09-16
  • 打赏
  • 举报
回复
错误提示为: lifei@lifei-Inspiron-N4050:~/C++/19$ g++ -Wall -std=c++11 -o proc proc2.cpp /tmp/cc3VwBq5.o: In function `main': proc2.cpp:(.text+0x17): undefined reference to `screen::Menu' proc2.cpp:(.text+0x1d): undefined reference to `screen::Menu' proc2.cpp:(.text+0x2d): undefined reference to `screen::Menu' proc2.cpp:(.text+0x33): undefined reference to `screen::Menu' proc2.cpp:(.text+0x43): undefined reference to `screen::Menu' /tmp/cc3VwBq5.o:proc2.cpp:(.text+0x49): more undefined references to `screen::Menu' follow collect2: error: ld returned 1 exit status
贰到不行 2014-09-16
  • 打赏
  • 举报
回复
找不到错误,编译指令为:g++ -Wall -std=c++11 -o proc proc2.cpp 把静态设置为私有的化就没有问题了

33,320

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧