编译代码时麻烦 - strncpy char*和string
Trouble while compiling code - strncpy char* and string
我对创建的类有一个问题。当我对此进行编译时,会出现错误消息。基本上是关于字符串的char*。这是我的班级
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
class Stock //klassdekleration
{
private:
char company[30];
int shares;
double share_val;
double total_val;
void set_tot() {total_val = shares * share_val;}
public:
Stock();
Stock(const char * co, int n = 0, double pr = 0.0);
void acquire(const char * co, int n, double pr);
void buy(int num,double price);
void sell(int num, double price);
void update(double price);
void show();
void compare( Stock );
const char * returncompany();
int returnshares();
};
#endif
// class function
//------------------------------------------------------------------------------------------------------
// constructor
Stock::Stock()
{
cout << "pre constructor is called n";
strcpy(company, "namnlöst");
shares = 0;
share_val = 0;
total_val = 0;
}
Stock::Stock(const char * co, int n, double pr)
{
cout << " Constructor that use " << co << " is called n";
strcpy("company", co);
company[29] = ' ';
shares = n;
share_val=pr;
set_tot();
}
//---------------------------------------------------------------------------------------------------------
// andra metoddefintioner
void Stock::acquire(const char * co, int n, double pr)
{
strcpy(company, co); //trunkera co om det behövs
company[29]=' ';
if (n<0)
{
cerr << "amount of shares cant be negative "
<< "share put to 0. n";
shares = 0;
}
else
shares = n;
share_val = pr;
set_tot();
}
void Stock::buy(int num, double price)
{
if (num < 0)
{
cerr << "Amount of bought shares cant be negative. "
<< "transaction cancelled. ";
}
else
{
shares += num;
share_val = price;
set_tot();
}
}
void Stock::sell(int num, double price)
{
if (num < 0)
{
cerr << "amount of sold shares cant be negative. "
<< "Transaction cancelled. ";
}
else if (num > shares)
{
cerr << "cant sell more shares than you posses"
<< "Transaction cancelled. ";
}
else
{
shares -= num;
share_val = price;
set_tot();
}
}
void Stock::update(double price)
{
share_val = price;
set_tot();
}
void Stock::show()
{
cout << "Company: " << company
<< " Shares: " << shares << " n"
<< " share value: $" << share_val
<< " Total value: $ " << total_val << " n";
}
int Stock::returnshares()
{
return shares;
}
const char * Stock::returncompany()
{
return company;
}
void Stock::compare(Stock stock2)
{
if (shares < stock2.returnshares())
{
cout << "Company" << stock2.returncompany() << "have higher share value than" << company;
}
else
{
cout << "Company" << company << "have higher share value than" << stock2.returncompany();
}
}
我收到此错误消息。
In constructor ‘Stock::Stock(const char*, int, double)’:
Stock_class.cc:60:23: warning: ISO C++ forbids converting a string constant to ‘char*’ [-Wwrite-strings]
strcpy("company", co);
知道我如何解决这个问题?
亲切的问候Hampus hahne
看来很明显,您写了
strcpy("company", co);
当您的意思
strcpy(company, co);
需要注意细节。
也
company[29]=' ';
是不必要的,因为strcpy
总是添加' '
字符,因此您也不需要添加一个字符。
我认为在您的代码strcpy(Company,Co)中会解决您的问题。使用strcpy不合适,请遵循以下链接以获取更多详细信息:https://www.geeksforgeeks.org/strcpy-in-cppp/
相关文章:
- 使用char类型将decimal转换为string,将string转换为decimal
- std::string 构造函数如何处理固定大小的 char[]?
- C++:将模板类型 char 转换为 std::string
- 从 std::string 到 std::array<char,size> 的 memcopy 额外数据是否是一种未定义的行为?
- "No suitable conversion function from 'std::string' to 'const char *' exists"
- 将"std::string {aka std::basic_string}"赋值中的不兼容类型<char>
- 错误:为"运算符 std::string {aka std::__cxx11::basic_string}"指定的返回类型<char>
- 为什么std::string没有(显式)const char*强制转换
- 将 std::string 转换为 const char* 用于 printf 消耗
- 我可以使用istream_iterator将<char>一些 istream 内容复制到 std::string 中吗?
- 让 'std::vector<unsigned char>' 从 'std::string' 中窃取内存
- 错误:请求从"Mstream"转换为非标量类型"std::string {aka std::basic_string<char>}"
- C :如何使用char / string 2D数组接受用户的1个以上的源性
- 我们应该在带有 char *func(char *string) 的函数中返回什么?需要简要说明这与 c 中的 char
- C++ 从 char* 到 char 的转换无效 (char* = *string.begin() )
- 无法在循环中读取Char/string
- 当我将char* string赋值给std::string时,可以释放它的内存吗?
- C char string和stint .h uint8_t在嵌入式系统中的兼容性
- 如何在c++中将LPBYTE转换为char */string
- 如何在等待用户输入int, float或其他类型时拒绝输入char/string