1
嘗試使用佈局新的,但它不斷給我錯誤。我記得前一陣子,它在工作。 Ubuntu 14.04上的g ++(ver 4.8.4)。C++佈局新不斷給編譯錯誤
#include <stdio.h>
typedef unsigned int uint;
struct strSession {
uint sessionId;
uint srcIp;
uint dstIp;
};
int main(int argc, char *argv[]) {
char buf[20];
strSession *q = (strSession*)&buf[0];
new (q) strSession;
return 0;
}
遇到錯誤
$ g++ -std=c++11 te.cc `pkg-config --cflags glib-2.0`
te.cc: In function ‘int main(int, char**)’:
te.cc:12:10: error: no matching function for call to ‘operator new(sizetype, strSession*&)’
new (q) strSession;
^
te.cc:12:10: note: candidate is:
<built-in>:0:0: note: void* operator new(long unsigned int)
<built-in>:0:0: note: candidate expects 1 argument, 2 provided
任何想法有什麼不對?
另外我不太明白'q'的意思。似乎擊敗對象。哈哈,可以這麼說。 –
@LightnessRacesinOrbit,同意。 –
需要注意對齊,介意你。 –