编译代码时麻烦 - strncpy char*和string

Trouble while compiling code - strncpy char* and string

本文关键字:char string strncpy 代码 麻烦 编译      更新时间:2023-10-16

我对创建的类有一个问题。当我对此进行编译时,会出现错误消息。基本上是关于字符串的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/

相关文章: