【问题标题】:cannot convert parameter 3 from 'std::vector<_Ty> *' to 'Inventory *'. Why?无法将参数 3 从 'std::vector<_Ty> *' 转换为 'Inventory *'。为什么?
【发布时间】:2012-08-04 04:19:07
【问题描述】:
#include "foodservice.h"
#include <iostream>
using namespace std;

int main() {
  Inventory Master;
  bool flag;
  Customer Bob("Bob", 12345, 100.00 );
  Customer Joe("Joe", 56789, 50.00 );
  Customer Arjun("Arjun", 98765, 00.01 );
  Customer Randy("Randy", 54689, 30.28);
  Customer Mark("Mark", 76598, 15.18);


  Master.firststock( "inventory.txt" );
  vector<Food> temp = Master._Inv;
  for(unsigned int i=0; i<temp.size(); i++ ) {
    cout << temp[i].name << " " << temp[i].quant << " " << temp[i].price << endl;
  }

  flag = Bob.addCart( "Apple" , 10,  &Master._Inv );
  Bob.report();
  flag = Bob.addCart( "Oranges", 2, &Master._Inv );
  flag = Bob.removeCart( "Apple", 3, &Master._Inv );
  flag = Arjun.addCart( "Apple", 1, &Master._Inv );
  flag = Bob.checkout(&Master._Inv);
  flag = Arjun.checkout(&Master._Inv);
  Master.summary();*/



  system("pause");

}

这是我的头文件的一部分:

class Inventory;
class Customer {
  public:
    Customer(string n, int c, double b );
    ~Customer() { _Cart.clear(); };
    bool addCart( string name, int q, Inventory* inv );
    bool removeCart( Food f, int q, Inventory* inv );
    void report(); 
    bool checkout(Inventory* inv); 
  protected:
    string name;
    int card;
    double balance;
    CreditCard _CC(int c,double b);
    vector<Food> _Cart;
};


The error i am getting is: cannot convert parameter 3 from 'std::vector<_Ty> *' to 'Inventory *'
1>          with
1>          [
1>              _Ty=Food
1>          ]
1>          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast

感谢您的帮助。所以当我使用 &Master._Inv 时会显示错误。 _Inv 是我在标题中其他地方声明但未包含的食物向量。但是问题出在指针 &Master.... 我也试过 *Master._Inv 但也没有用。

【问题讨论】:

  • 您使用的是reserved identifiers。你应该改掉这个习惯。其他挑剔的是using namespace std;、system("PAUSE");,事实上Customer 的析构函数完全没有意义,因为向量无论如何都会死。
  • 您的编译器是否为错误打印一些行号?如果没有,您是否逐行删除,直到确定错误行?

标签: c++ visual-c++


【解决方案1】:

错误消息非常简单。 addCart、removeCart 和 checkout 都将指向 Inventory 的指针作为参数。但是你的论点&amp;Master._Inv 是一个指向std::vector&lt;Food&gt; 的指针。也许你的意思只是&amp;Master?

【讨论】:

  • 感谢您的反馈
【解决方案2】:

Customer::addCart() 的第三个参数是一个指向 Inventory 对象的指针。尝试通过它&大师。

【讨论】:

  • 感谢您的反馈
猜你喜欢
  • 1970-01-01
  • 2017-06-19
  • 2011-04-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 2020-02-26
相关资源
最近更新 更多