gpt4 book ai didi

C++ 错误字符分配

转载 作者:行者123 更新时间:2023-11-30 05:01:39 26 4
gpt4 key购买 nike

赋值字符错误:

structplusclass.cpp: In constructor ‘Soldado::Soldado(char, unsigned int, char, char, char)’:
structplusclass.cpp:25:27: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
strcpy(mTodos.modelo, mol);
^
In file included from /usr/include/c++/5/cstring:42:0,
from structplusclass.cpp:2:
/usr/include/string.h:125:14: note: initializing argument 2 of ‘char* strcpy(char*, const char*)’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
structplusclass.cpp:27:29: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
strcpy(mTodos.material, mat);
^
In file included from /usr/include/c++/5/cstring:42:0,
from structplusclass.cpp:2:
/usr/include/string.h:125:14: note: initializing argument 2 of ‘char* strcpy(char*, const char*)’
extern char *strcpy (char *__restrict __dest, const char *__restrict __src)
^
structplusclass.cpp:28:18: error: invalid conversion from ‘char’ to ‘const char*’ [-fpermissive]
strcpy(mNome, ns);

代码:

#include <iostream>
#include <cstring>
#define SIZE 20

using namespace std;

struct faca {
char modelo[SIZE];
unsigned int peso;
char material[15];
};

class Soldado {
public:
Soldado (char ns, unsigned int p, char mat, char arm, char mol);
~Soldado ();
void Imprime();
protected:
faca mTodos;
char mNome[SIZE];
char mArmapri[SIZE];
};

Soldado::Soldado (char ns, unsigned int p, char mat, char arm, char mol) {
strcpy(mTodos.modelo, mol);
mTodos.peso = p;
strcpy(mTodos.material, mat);
strcpy(mNome, ns);
strcpy(mArmapri, arm);
}

void Soldado::Imprime () {
cout << "Soldado : " << mNome << ", " << "firearm : "
<< mArmapri << endl;
cout << endl << " Faca : " << mTodos.modelo << ", "
<< "lenght : " << mTodos.peso << ", " << "Material : "
<< mTodos.material << endl;
}

Soldado::~Soldado () {
mTodos.peso = 0;
}

int main() {
char names1[SIZE], fire[SIZE];
char names2[SIZE], mat[15];
unsigned int eight;
cout << "nome Soldado, nome arma de fogo " << endl;
cin >> names1 >> fire;
cout << "modelo faca e material " << endl;
cin >> mat >> names2;
cout << "peso" << endl;
cin >> eight;
Soldado brazil(names1, eight, mat, fire, names2);
brazil.Imprime();



return 0;
}

最佳答案

Soldado::Soldado() 采用了错误的参数。

char names1[SIZE], fire[SIZE];

cout << "nome Soldado, nome arma de fogo " << endl;
cin >> names1 >> fire;

Soldado brazil(names1, ..., ..., fire, ...);

这里 names1fire 都声明为 char[SIZE] 类型。这意味着内存中有 SIZE 类型的 char 项目排成一行(具体来说,在堆栈上,但这并不重要) .但是随后您将这些传递给您的构造函数,并且您的构造函数期望这样:

Soldado::Soldado (char ns, ..., ..., char arm, ...) { ... }

这里,nsarm被声明为char类型,是char类型的单个项而不是像上面那样列出它们。

解决此问题的最佳方法是将 nsarm(以及其他需要单词的参数)从 char< 类型更改为char[SIZE]。这将更正您的编译器错误,并让您的代码正常工作。

但是,还有一点。你的构造函数有几行是这样的:

strcpy(mTodos.modelo, mol);

只要您的用户从不输入超过 19 个字母的名称(留下一个 char 来标记结束),这些就可以完美地工作。但是,如果他们确实输入了更长的名称,您的代码将愉快地写入您为其保留的内存的末尾。你应该像这样使用 `strncpy() 函数来保护你的内存:

strncpy(mTodos.modelo, mol, SIZE);
mTodos.modelo[SIZE - 1] = '\0';

不幸的是,您需要第二行来确保字符串以 null 字符结尾,因为 strncpy() 不会总是为您做这件事。

关于C++ 错误字符分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50195906/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com