C++ 为什么C++;将赋值(=)视为重载运算符?

C++ 为什么C++;将赋值(=)视为重载运算符?,c++,compiler-errors,C++,Compiler Errors,为什么会出现此错误: 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::f

为什么会出现此错误:

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;
}
#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;
};
#包括“test.h”
Test::Test(){}
无效测试::foo1()
{
int i;
a_list=(a*)malloc(10*sizeof(a));
对于(i=0;i<10;i++)
a_list[i]=foo2();
}
}
A*测试::foo2()
{
A*A;
a=(a*)malloc(sizeof(a));
返回a;
}
Test.h:

#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;
}
#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;
};
#包括
#包括
#包括
使用名称空间std;
类型定义结构
{
双x;
双y;
字符串z;
}A;
课堂测试
{
公众:
测试();
void foo1();
私人:
A*foo2();
A*A_列表;
};
foo2()
返回指向
a
的指针,但
a\u列表[i]
a
类型的对象

另外,如果您使用
new
来分配动态内存,而不是
malloc
,则会更好

而不是:

a_list = ( A* ) malloc ( 10 * sizeof ( A ) ); 
你可以有:

a_list = new A[10];
要释放内存,请使用

delete [] a_list; 

<> >更好的选择是使用<代码> STD::vector < /p> >使用“代码> > STD::String < /C> >结构,它不是一个普通类型——它的构造函数不会被调用。你为什么用C++编写C代码而不是编写C++代码?别人的代码(OMXPoPE)@千禧年虫我很欣赏,但是错误发生在for循环中,可能它在所有的括号中都丢失了。我可以把
new
换成
malloc
=P吗?你能演示一个小脚本来帮我完成这项工作吗?我不知道
new
可以像well@puk-结构和类之间的唯一区别是默认值可见性:公共结构,类为私有。@ PUK在C++中,结构和类是相同的,唯一的区别是在Stult中,如果你不提及任何访问类型,那么默认情况下成员是公共的,而在一个类中,如果你不提及任何访问类型,默认情况下成员是私有的。当您调用初始化所有条目的
a_list=newa[10]
时,此时我只想分配空间。