所以我有这些代码行:
int maxY, maxX;
getmaxyx(stdscr, &maxY, &maxX);
Run Code Online (Sandbox Code Playgroud)
它给了我以下错误:
error C2440: '=' : cannot convert from 'int' to 'int *'
Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
Run Code Online (Sandbox Code Playgroud)
每次我使用它两次.我甚至没有使用=运算符!包含curses.h文件.我究竟做错了什么?
据我所知,如果你正在分配内存以临时存储某些内容,比如响应用户操作,并且当代码再次到达那一点时你就不再需要内存了,你应该释放内存以便它不会不会造成泄漏.如果不清楚,这里有一个例子,我知道释放内存很重要:
#include <stdio.h>
#include <stdlib.h>
void countToNumber(int n)
{
int *numbers = malloc(sizeof(int) * n);
int i;
for (i=0; i<n; i++) {
numbers[i] = i+1;
}
for (i=0; i<n; i++) {
// Yes, simply using "i+1" instead of "numbers[i]" in the printf would make the array unnecessary.
// But the point of the example is using malloc/free, so pretend it makes sense to use one here.
printf("%d ", numbers[i]);
}
putchar('\n');
free(numbers); // Freeing is absolutely necessary here; …Run Code Online (Sandbox Code Playgroud) 我创建了一个名为SkipToChar的类,它应该能够按如下方式使用:
std::ostringstream oss;
oss << "Hello," << SkipToChar(7) << "world!" << std::endl;
Run Code Online (Sandbox Code Playgroud)
哪个会打印"Hello,world!" (注意空格.)基本上它应该使用空格跳转到指定索引处的字符.但显然编译器无法识别operator<<我为它创建的.有趣的是,呼吁operator<<明确,即使没有给予任何模板参数(如operator<<(oss, SkipToChar(7));工作正常;它只是不,如果我实际的工作
这是我的代码:
#include <iostream>
#include <sstream>
template <typename _Elem>
struct basic_SkipToChar
{
typename std::basic_string<_Elem>::size_type pos;
basic_SkipToChar(typename std::basic_string<_Elem>::size_type position)
{
pos = position;
}
};
template <typename _Elem>
inline std::basic_ostringstream<_Elem> &operator<<(std::basic_ostringstream<_Elem> &oss, const basic_SkipToChar<_Elem> &skip)
{
typename std::basic_string<_Elem>::size_type length = oss.str().length();
for (typename std::basic_string<_Elem>::size_type i = length; i < skip.pos; i++) {
oss << (_Elem)' ';
}
return oss;
}
typedef …Run Code Online (Sandbox Code Playgroud) 这是一些重现该问题的简单代码:
#include <box2d/box2d.h>
int main()
{
b2World world(b2Vec2_zero);
b2BodyDef bdef;
b2Body* body = world.CreateBody(&bdef);
body->SetUserData(body);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
这应该根据我读过的所有内容进行编译,并且(对于学究来说)我猜它在技术上确实可以编译,但是当我尝试(使用g++ test.cpp -lbox2d)时,我收到链接器错误:
/usr/bin/ld: /tmp/ccgHfvqv.o: in function `main':
test.cpp:(.text+0x75): undefined reference to `b2Body::SetUserData(void*)'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
我尝试用谷歌搜索,"undefined reference to b2Body::SetUserData(void*)"但没有找到结果。
我也尝试通过设置它b2BodyDef,但userData该结构中的成员似乎工作方式不同,指向b2BodyUserData具有单个pointer成员的结构,该结构似乎没有设计用于保存用户数据指针,因为在那里设置地址会导致Box2D 稍后写入该地址,从而损坏数据。(我使用 GDB 观察点来检查这一点。)