C++ 为什么我的代码在代码块下运行,而不是在VS Studio中
这一行不能在VS Studio下通过,但它在代码块下运行 cg1.RegisterGoodsc++,23,32 “void CGoods::RegisterGoodschar[],int,float”:无法将参数1从“const char[4]”转换为“char[]” 像这样:C++ 为什么我的代码在代码块下运行,而不是在VS Studio中,c++,C++,这一行不能在VS Studio下通过,但它在代码块下运行 cg1.RegisterGoodsc++,23,32 “void CGoods::RegisterGoodschar[],int,float”:无法将参数1从“const char[4]”转换为“char[]” 像这样: #define _CRT_SECURE_NO_WARNINGS #include<iostream> #include <cstring> using namespace std; class
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
#include <cstring>
using namespace std;
class CGoods
{
private:
char Name[21];
int Amount;
float Price;
float Total_value;
public:
void RegisterGoods(char name[], int amount, float price)
{
strcpy(Name,name);
Amount = amount;
Price = price;
}
void CountTotal(void)
{
Total_value = Price * Amount;
}
void GetName(char name[])
{
strcpy(name,Name);
}
int GetMount(void)
{
return Amount;
}
float GetPrice(void)
{
return Price;
}
float GetTotal(void)
{
return Total_value;
}
};
int main() {
CGoods cg1;
cg1.RegisterGoods("c++", 23, 32);
cout<<cg1.GetPrice()<<endl;
cout<<cg1.GetMount();
return 0;
}
<>不要用C构造来解决问题,C++有更好的答案。字符指针可能导致不必要的行为和严重的缓冲区溢出攻击等。最好是使用一种新的方法
将您的成员函数注册表项更改为:
以及您的姓名声明:
将函数GetName返回到:
或
还添加include for std::string:
更好的代码提示。std是一个巨大的名称空间。无意中,您可能会覆盖std之外的函数,并最终导致几乎不可调试的错误
也将SET函数中的参数定义为const,因此不能无意地改变它的值。
不要使用C构造来处理事物,C++有更好的答案。字符指针可能导致不必要的行为和严重的缓冲区溢出攻击等。最好是使用一种新的方法 将您的成员函数注册表项更改为: 以及您的姓名声明: 将函数GetName返回到: 或 还添加include for std::string: 更好的代码提示。std是一个巨大的名称空间。无意中,您可能会覆盖std之外的函数,并最终导致几乎不可调试的错误 还可以将setter函数中的参数定义为const,这样您就不会无意中更改它的值。char name[]作为函数参数等同于char*name,而字符串文字的类型为const char[4],只能安全地转换为const char*,因此您必须按如下方式更改参数:void RegisterGoods(const char *name, int amount, float price)
private:
std::string Name;
public:
void RegisterGoods(const string name, int amount, float price)
{
Name=name;
Amount = amount;
Price = price;
}
const std::string GetName()
{
return Name;
}
在这里:
// Renamed to SetName given that it's what this function actually does
void SetName(const char *name)
一般来说,虽然不应该使用纯字符数组来存储C++中的字符串,但您应该更喜欢使用STD::Str:
char name[]作为函数参数等同于char*name,而字符串文字的类型为const char[4],只能安全地转换为const char*,因此必须按如下方式更改参数:void RegisterGoods(const char *name, int amount, float price)
private:
std::string Name;
public:
void RegisterGoods(const string name, int amount, float price)
{
Name=name;
Amount = amount;
Price = price;
}
const std::string GetName()
{
return Name;
}
在这里:
// Renamed to SetName given that it's what this function actually does
void SetName(const char *name)
一般来说,虽然不应该使用纯字符数组来存储C++中的字符串,但您应该更喜欢使用STD::Str:
这是不是意味着我们不再需要C++中的Strucy和Car?因为我用字符串和它的函数替换了所有的“char”。像这样:
void RegisterGoods(const char *name, int amount, float price)
private:
std::string Name;
public:
void RegisterGoods(const string name, int amount, float price)
{
Name=name;
Amount = amount;
Price = price;
}
const std::string GetName()
{
return Name;
}
这是不是意味着我们不再需要C++中的Strucy和Car?因为我用字符串和它的函数替换了所有的“char”。像这样:
void RegisterGoods(const char *name, int amount, float price)
private:
std::string Name;
public:
void RegisterGoods(const string name, int amount, float price)
{
Name=name;
Amount = amount;
Price = price;
}
const std::string GetName()
{
return Name;
}
请不要为字符串文本传递数组,请使用const char*isntead。请改用std::string*这可能是因为代码块使用不同的编译器或编译器设置,并且不强制执行这些设置。但是像C++这样的字符串文字是conchar(4),并将其作为char *违反const正确性。因此,这应始终作为错误处理,即使如此,编译器和/或IDE在其默认设置中通常仅将其作为警告处理。请不要为字符串文本传递数组,请使用const char*isntead。改用std::string*这可能是因为代码块使用不同的编译器或编译器设置,并且不强制执行该设置。但是像C++这样的字符串文字是conchar(4),并将其作为char *违反const正确性。所以这应该被当作一个错误来处理,即使如此,编译器和/或IDE在其默认设置中通常只将其作为警告处理type@StackDanny将您的评论包括在我的回答中我是说std::string GetName const而不是const std::string GetName谢谢您的编辑,但是参数的常数并不是不必要的。您可以纠正我,但我拒绝更改它们是不必要的,因为参数不是通过引用发送的。Being按价值发送无论如何都会复制它们。但是您当然可以将其保留为const,因为良好实践有一个很好的理由,那就是避免像前面讨论的那样意外地分配参数。函数GetNamevoid可以声明为const,不需要返回type@StackDanny在我的回答中包含您的评论我是说std::string GetName const而不是conststring getname感谢您的编辑,但是参数的常量并不是不必要的。您可以纠正我,但我拒绝更改它们是不必要的,因为参数不是通过引用发送的。Being按价值发送无论如何都会复制它们。但是你当然可以保持常量,因为良好实践有一个很好的理由,那就是避免像前面讨论的那样意外地分配参数。请不要建议按值传递字符串。最好使用const引用。@skratchi.at,如果在引入移动语义后存储了该值,则实际传递值应该是默认选项。阅读更多,请不要建议
按值传递字符串。最好使用const引用。@skratchi.at,如果在引入移动语义后存储了该值,则实际传递值应该是默认选项。阅读更多。