2016-07-25 6 views
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 

任意のアイデアは間違って何ですか?

答えて

6

配置newを使用するには、あなたが持っている必要があります:あなたは同じように簡単に使用することもでき

#include <new> 

はまた、:

int main(int argc, char *argv[]) { 
    char buf[20]; 
    strSession *q = new (buf) strSession; 
    return 0; 
} 
+0

また、私は 'q'のポイントをあまり得ていません。オブジェクトを倒すと思われる。ハハ、そう言います。 –

+0

@LightnessRacesinOrbit、同意します。 –

+0

アライメントを監視する必要があります。 –

1

をあなたの元のコードの作業を行うには、追加する必要が

void* operator new(size_t, strSession * p) { return p; } 

C++がBell Labsを退社する以前は、C++の機能は でしたコンストラクタは 'this'に代入することができます。演算子の新しい配置 の構文は改善と見なされました。

関連する問題