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

时间:2019-03-12 09:38:17

标签: c++

我创建的课程有问题。当我编译时,会发生错误消息。它基本上全部关于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] = '\0';
  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]='\0';
  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);

您知道如何解决此问题吗?

亲切的问候 汉普斯犬

0 个答案:

没有答案
相关问题