爲什麼我得到這個錯誤:爲什麼C++將賦值(=)視爲重載運算符?
test.cpp:11:28: error: no match for ‘operator=’ in ‘*(((Test*)this)->Test::a_list + ((unsigned int)(((unsigned int)i) * 20u))) = Test::foo2()’
當我編譯下面的代碼(通過g++ test.cpp -o test
)
TEST.CPP:
#include "test.h"
Test::Test() {}
void Test::foo1()
{
int i;
a_list = (A*) malloc (10 * sizeof (A));
for (i = 0; i < 10; i++)
a_list [ i ] = foo2();
}
}
A* Test::foo2()
{
A *a;
a = (A*) malloc (sizeof (A));
return a;
}
Test.h:
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
using namespace std;
typedef struct
{
double x;
double y;
string z;
} A;
class Test
{
public:
Test();
void foo1();
private:
A* foo2();
A *a_list;
};
使用了'malloc'分配包含'的std :: string',這不是一個簡單的結構類型 - 其構造不會被調用。 – milleniumbug
你用C++編寫C代碼而不是編寫C++代碼的任何原因? – greatwolf
別人的代碼(omxplayer) – puk