假设我有课程Foo
并Bar
设置如下:
class Foo
{
public:
int x;
virtual void printStuff()
{
std::cout << x << std::endl;
}
};
class Bar : public Foo
{
public:
int y;
void printStuff()
{
// I would like to call Foo.printStuff() here...
std::cout << y << std::endl;
}
};
Run Code Online (Sandbox Code Playgroud)
正如在代码中注释的那样,我希望能够调用我所覆盖的基类函数.在Java中有super.funcname()
语法.这在C++中是否可行?
据我所知,没有理由不允许我在C++中传递对指针的引用.但是,我这样做的尝试失败了,我不知道为什么.
这就是我正在做的事情:
void myfunc(string*& val)
{
// Do stuff to the string pointer
}
// sometime later
{
// ...
string s;
myfunc(&s);
// ...
}
Run Code Online (Sandbox Code Playgroud)
我收到这个错误:
无法将参数1从'std :: string*'转换为'std :: string*&'
有许多程序,例如Visual Studio,可以检测外部程序何时修改文件,然后在用户想要选择时重新加载文件.是否有一种相对简单的方法在C++中执行此类操作(不一定必须与平台无关)?
我有一个macbook我正在尝试做一些开发.我有一个我想要构建的程序,当我使用make
它来构建它时,我得到了一个"找不到命令"的错误.我做了一些google和SO搜索,看起来这不是一个常见的问题.为什么我没有安装,我怎么得到它?我更加困惑,因为我知道我最近使用它(在过去一个月左右),当我在这台笔记本电脑上.
我正在研究如何在C++中将成员的内存偏移量转换为类,并在维基百科上看到了这一点:
在C++代码中,您不能使用offsetof来访问非Plain Data Data Structures的结构或类的成员.
我尝试了它似乎工作正常.
class Foo
{
private:
int z;
int func() {cout << "this is just filler" << endl; return 0;}
public:
int x;
int y;
Foo* f;
bool returnTrue() { return false; }
};
int main()
{
cout << offsetof(Foo, x) << " " << offsetof(Foo, y) << " " << offsetof(Foo, f);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到了一些警告,但它已经编译,运行时它给出了合理的输出:
Laptop:test alex$ ./test
4 8 12
Run Code Online (Sandbox Code Playgroud)
我想我要么误解POD数据结构是什么,要么我错过了其他一些难题.我不知道问题是什么.
我正在尝试从我在Lua中使用的库中包装一个类.特别是,我正在尝试从SFML中包装颜色类.在这里和这里可以看到颜色类的完整源代码.
这就是我失败的功能.
int SFColor_new(lua_State* L)
{
// omitting part where I set r, g, b, and a
new (lua_newuserdata(L, sizeof(Color))) Color(r, g, b, a); // Line 47
luaL_getmetatable(L, LuaInfo<Color>::myMetaTableName);
lua_setmetatable(L, -2);
return 1;
}
Run Code Online (Sandbox Code Playgroud)
这就是错误
LuaSFMLColor.cpp: In function ‘int ag::SFColor_new(lua_State*)’:
LuaSFMLColor.cpp:47: error: no matching function for call to ‘operator new(unsigned int, void*)’
<built-in>:0: note: candidates are: void* operator new(unsigned int)
make: *** [game] Error 1
Run Code Online (Sandbox Code Playgroud)
我在其他几个地方做类似的事情而没有遇到这个错误,所以我不确定是什么原因导致它.看看Color的源代码,我没有看到任何奇怪或不同寻常的东西,而且我已经没有想法了.我也尝试使用默认构造函数(即没有参数)然后只是设置值,但这也没有任何好处.
我创建了一个文件hi.cpp,我写了下面给出的命令:
#include <iostream>
using namespace std;
int main ()
{
cout << "Hello World! ";
cout << "I'm a C++ program";
return 0;
}
Run Code Online (Sandbox Code Playgroud)
然后我使用以下命令在我的RHEL 6机器上运行它
gcc hi.cpp
Run Code Online (Sandbox Code Playgroud)
我有一些错误如下:
[chankey@localhost ~]$ gcc hi.cpp
/tmp/cc32bnmR.o: In function `main':
hi.cpp:(.text+0xa): undefined reference to `std::cout'
hi.cpp:(.text+0xf): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
hi.cpp:(.text+0x19): undefined reference to `std::cout'
hi.cpp:(.text+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, const char*)'
/tmp/cc32bnmR.o: In function `__static_initialization_and_destruction_0(int, int)': …
Run Code Online (Sandbox Code Playgroud) 我正在使用Robot类在Java中创建一个小程序.该程序接管鼠标.在调试过程中,如果它开始以我不想要的方式运行,很难退出程序,因为我无法将鼠标移动到eclipse中的终止按钮,我不能使用热键来点击它是因为鼠标在另一个窗口中不断点击,而是让窗口聚焦.
我想做的就是连接一个keylistener,这样当我点击q时我可以退出该程序,但我知道如何做到这一点的唯一方法是创建一个窗口,该窗口需要焦点来捕获输入.有没有办法从任何地方听取键盘或鼠标输入,无论焦点是什么?
在使用Vim时,我有时会想要查看函数定义或结构定义,因此我将C-]
跳转到它.但是,我遇到了一些问题.首先,我不知道如何轻松地跳回去.看来我之前的文件已关闭,而我现在处于新文件中.有没有办法跳回来,或保留一堆打开的文件,我可以弹回或什么?
另一件事我注意到,当我对当前文件进行更改时,我需要保存它,因为就像前面提到的那样,我的当前文件在下一个文件打开之前被关闭.
有时我想立刻查看我当前的代码和标题.有没有办法在拆分中打开标签定义?
我的.vimrc中有以下几行,我认为这样就足够了:
nnoremap <left> <nop>
nnoremap <right> <nop>
nnoremap <up> <nop>
nnoremap <down> <nop>
inoremap <left> <nop>
inoremap <right> <nop>
inoremap <up> <nop>
inoremap <down> <nop>
Run Code Online (Sandbox Code Playgroud)
箭头键在正常模式下不执行任何操作(如预期),但在插入模式下继续运行.我也尝试过imap
,这也不起作用.查询映射inoremap <left>
确认绑定不会被其他内容覆盖.我不确定发生了什么.
作为最后一次测试,我启动了vim而没有vimrc vim -u NONE
,然后只运行了映射.如果我现在进入插入模式并按箭头键,它会在我光标所在的新行上插入字母A,B,C或D,这完全不是我所期望的.
c++ ×6
vim ×2
ctags ×1
file-io ×1
filesystems ×1
fsevents ×1
java ×1
keylistener ×1
macos ×1
makefile ×1
monitoring ×1
new-operator ×1
offsetof ×1
overriding ×1
pointers ×1
reference ×1
split ×1
terminal ×1