无法从int转换为vector

时间:2014-04-19 10:05:52

标签: c++ oop vector

以前我问了一个问题并得到了一个小答案,但现在我已经达到了一个我无法成功编译此代码的程度。我正在尝试构建一个商店类型数据库,现在我可以简单地使用一个字符串数组。但我一直建议使用字符串数组是在C ++中使用的不好的做法,并不是一个好主意继续。因此,为了将来的参考,我希望这样做,以便我可能稍后在我的考试中处理载体。

 #include "string"
 #include "vector"
 #include "iostream"
 using namespace std;
 class shop
 {
    private:
         int i, n, item[20];
         float price[20];
         std::vector<std::string> name;
     public:
         void input();
         void output();
 };
 void shop::input()
 {
     cout << "Enter the number of items: ";
     cin >> n;
     name.clear();
     name.push_back(n);
     name.resize(n);
     for(i = 1; i <= n; i++)
     {
         cout << "Enter the item number of the " << i << " item: ";
         cin >> item[i];
         cout << "Enter the name of the item: ";
         cin >> name[i];
         cout << "Enter the price of the item: ";
         cin >> price[i];
     }
 }
 void shop::output()
 {
     for(i = 1; i <= n; i++)
     {
         cout << "Item Number: " << item[i] << endl;
         cout << "Price: " << price[i] << endl << endl;
         cout << "Name: " << name[i] << endl << endl;
     }
 }
 void main()
 {
     class shop s;
     s.input();
     s.output();
 }

但我得到的错误是:

1>------ Build started: Project: Project1, Configuration: Debug Win32 ------
1>  Source.cpp
1>c:\users\khale_000\documents\visual studio 2013\projects\project1\project1\source.cpp(20): error C2664: 'void std::vector<std::string,std::allocator<_Ty>>::push_back(const std::basic_string<char,std::char_traits<char>,std::allocator<char>> &)' : cannot convert argument 1 from 'int' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>> &&'
1>          with
1>          [
1>              _Ty=std::string
1>          ]
1>          Reason: cannot convert from 'int' to 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
1>          No constructor could take the source type, or constructor overload resolution was ambiguous
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

2 个答案:

答案 0 :(得分:1)

您正在读取整数n并试图将其推入字符串向量。编译器的错误非常清楚。如果要将其转换为字符串,请使用std::to_string。如果您不能使用C ++ 11,请使用snprintf(buffer, size, "%d", n);

答案 1 :(得分:1)

您将向量定义为std::string

类型的对象的向量
std::vector<std::string> name;

但是试图在其中推送一个整数。

 cin >> n;
 name.clear();
 name.push_back(n);

此代码也无效

 for(i = 1; i <= n; i++)
 {
     cout << "Enter the item number of the " << i << " item: ";
     cin >> item[i];
     cout << "Enter the name of the item: ";
     cin >> name[i];
     cout << "Enter the price of the item: ";
     cin >> price[i];
 }

因为你可以写出超出数组的项目和价格。您不检查n是否大于或等于20,即数组的大小。实际上代码没有意义。

我会按照以下方式编写

class shop
 {
    private:
         struct article
         {
              int item;
              float price;
              std::string name;
         };
         std::vector<article> articles;
     public:
         void input();
         void output const();
 };

void shop::input()
{
     cout << "Enter the number of items: ";
     int n;
     cin >> n;

     articles.clear();
     articles.reserve( n );

     for ( int i = 1; i < n; i++ )
     {
         article a;
         cout << "Enter the item number of the " << i << " item: ";
         cin >> a.item;
         cout << "Enter the name of the item: ";
         cin >> a.name;
         cout << "Enter the price of the item: ";
         cin >> a.price;
         articles.push_back( a );
     }
 }

考虑到我使用的是成员函数reserve而不是resize,就像你的函数一样。