Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/css/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 快速新操作员问题_C++ - Fatal编程技术网

C++ 快速新操作员问题

C++ 快速新操作员问题,c++,C++,我知道这很容易,我正在看一些东西,但这就是我所拥有的…: typedef struct { char s1[81]; char s2[81]; char s3[81]; }Rec; int main() { Rec *a[10]; a[0] = (Rec*)new unsigned char(sizeof(Rec)); a[0]->s1= "hello"; printf("a[0] = %s\n",a[0]->s1);

我知道这很容易,我正在看一些东西,但这就是我所拥有的…:

typedef struct
{
    char s1[81];
    char s2[81];
    char s3[81];
}Rec;

int main()
{   

   Rec *a[10];

   a[0] = (Rec*)new unsigned char(sizeof(Rec));
   a[0]->s1= "hello";
   printf("a[0] = %s\n",a[0]->s1);
   delete(a[0]);


   getchar();
   return 0;
}
现在,排队

a[0]->s1=“你好”


正在抱怨的表达式必须是可修改的左值。我很确定这就是我在我的新操作符行中使用它的方式,它需要一个长值或其他什么,但我不确定要用什么代码来实现这一点。。。很简单,我知道,但是是的。任何帮助都将不胜感激

您不能这样分配到字符数组。使用strcpy,或者将字符数组更改为
std::string

strcpy(a[0]->s1, "hello");
你为什么这样做:

a[0] = (Rec*)new unsigned char(sizeof(Rec));
与此相反:

a[0] = new Rec;

问题不在于你的演员。新表达式分配一个
无符号字符
并将其初始化为
sizeof(Rec)
,而不是将足够的空间分配为
new unsigned char[sizeof(Rec)]就可以了。也就是说,
s1
“hello”
的类型是不同的,您不能将一种类型分配给另一种类型。您应该使用类似于strcpy的
strcpy
,但是既然您标记了这个C++,那么您最好使用
std::string
。另外,为什么不直接调用
newrec

a[0]是指向无法修改的字符数组的指针——a[0]将始终指向同一地址。 您需要使用strcpy将“hello”字符串复制到[0]

两件事。线路

a[0] = (Rec*)new unsigned char(sizeof(Rec));
分配一个
无符号字符
,该字符初始化为值
sizeof(Rec)
。你可能是说

a[0] = (Rec*)new unsigned char[sizeof(Rec)];
或者更好

a[0] = new Rec;
其次,不能将字符串文字分配给字符数组,需要逐个复制字符,例如

char s[80];
s = "hello"; // won't work
strcpy(s, "hello"); // correct

不过,在这种情况下,您应该使用
std::string

我想您在生活中已经使用了很多C语言。请记住C++是不同的语言,它与C的大部分语法和一些标准库共享。这意味着C中完全完美的东西在C++中可能非常丑陋(甚至危险)。 话虽如此,让我们用更“C++-ish”的方式重写代码:

#包括//std::cout,std::endl
#include//std::string
C++/TyPrIFF结构是C++中的结构
{
std::string s1;//使用std::string而不是char数组
std::字符串s2;
std::字符串s3;
}; // 别忘了分号!
int main()
{   
记录*a[10];
a[0]=new Rec;//分配正确的内存量,无需强制转换
[0]->s1=“hello”//std::sring为您处理分配

STD::什么是旧的学校代码?TyBuffFr.{…} ReC;< /Cord>构造?@约翰,我发誓,C和C++之间的差别不够大。<代码>新< /C> >比 MalOC/ >更强大,使用它:<代码> A[ 0 ] =新Read()
我猜他做了很多C,只是用新的无符号字符替换了
malloc
a[0]
是指向Rec的指针。如果你是说
a[0]>s1
,那不是指向数组的指针,而是数组。
#include <iostream> // std::cout, std::endl
#include <string> // std::string

struct Rec // typedef is implicit for structs in C++
{
    std::string s1; // use std::string instead of char arrays
    std::string s2;
    std::string s3;
}; // don't forget the semicolon!

int main()
{   

   Rec * a[10];

   a[0] = new Rec; // allocates the right amount of memory, no need to cast
   a[0]->s1 = "hello"; // std::sring handles the assignment for you
   std::cout << "a[0] = " << a[0]->s1 << std::endl; // use iostreams
   delete a[0]; // delete is an operator, not a function, no need for parentheses

   getchar(); // warning, this is not portable
   return 0;
}