簡體   English   中英

如何將任何 class 的對象向量傳遞給非成員 function 作為參考?

[英]How to pass a vector of objects of any class to a non-member function as a reference?

這是我嘗試過的

在 function 的原型設計中

void display(const std::vector<movies>& v);

function定義中

void display(const movies std::vector<movies>& v){
  std::cout<<v.get_name();
}

function 調用

display(list1);

當我運行這個我得到這些錯誤

  movies.cpp:52:46: error: expected unqualified-id before ‘&’ token
  void display(const movies std::vector<movies>& v){
                                               ^
  movies.cpp:52:46: error: expected ‘)’ before ‘&’ token
  movies.cpp:52:48: error: expected initializer before ‘v’
  void display(const movies std::vector<movies>& v){
                                                 ^

根據@aschepler 在評論中的建議進行編輯。 如果我從 function 定義中刪除額外的“電影”。 現在 function 定義變為

void display(const std::vector<movies>& v){
  std::cout<<v.get_name();
}

它仍然無法編譯,現在它顯示了一些不同的錯誤消息。

movies.cpp: In function ‘void display(const std::vector<movies>&)’:
movies.cpp:53:16: error: ‘const class std::vector<movies>’ has no 
member named ‘get_name’  std::cout<<v.get_name();
                                      ^~~~~~~~~~

如果您想通過它 go,我的完整代碼也在下面給出。

我也是初學者,所以請善待。

我的 class 電影的定義

  movies.h file
  #pragma once
  #include<string>
  #include<vector>
    class movies{
      private:
   std::string name;
   double rating;
   int times_watched;
  public:
   static int num_movies;
  movies();
  ~movies();
   movies(std::string,double,int);
   void set_name(std::string);
   void set_rating(double);
   void set_times_watched(int);
   std::string get_name() const;
   double get_rating() const;
   int get_watch_history() const;
  };
 void display(const std::vector<movies>& v);

class 的實現

  movies.cpp file
  #include "movies.h"
  #include<iostream>
  int movies::num_movies=0;
  movies::movies()
   :name{"null"},rating{0},times_watched{0}{
      num_movies++;
  }
 movies::~movies(){
    num_movies--;
 }
 void movies::set_times_watched(int t){
    times_watched=t;
 }
 void movies::set_name(std::string nm){
     this->name=nm;
 }
void movies::set_rating(double d){
   this->rating =d;
}
std::string movies::get_name() const{
 return this->name;
}
double movies::get_rating() const{
return this->rating;
}
int movies::get_watch_history() const{
   return this->times_watched;
   }
  movies::movies(std::string na,double k,int s)
    :name{na},rating{k},times_watched{s}{
    num_movies++;
    }
    bool rating_check(double rt){
        if((rt<=5)&&(rt>=0)){
           return true;
          }else{
         return false;
       }
    }
   bool times_watched_check(int watched){
     if(watched>=0){
         return true;
       }else{
        return false;
       }
     }
void display(const movies std::vector<movies>& v){
     std::cout<<v.get_name();
   }

主程序是

main.cpp file
#include<iostream>
#include<vector>
#include<string>
#include "movies.h"
int main(){
 std::vector<movies>list1 (1,movies());
 std::string temp {"null"},temp1;
 int num=0,flag=0;
 double temp2;
 while(temp != "n"){

 std::cout<<"enter name:";
 std::cin>>temp1;
 if(num!=0){
     for(int j=0;j<=num;j++){
         if(temp1==list1[j].get_name()){
             std::cout<<"movie already present"<<std::endl;
             flag=1;
             break;
            }
        }
    }
    if(flag==0){
         list1[num].set_name(temp1);
          temp1.clear();
    rating:
          std::cout<<"enter your rating of movie(on a scale of 5):";
          std::cin>>temp2;
          if(rating_check(temp2)){
              list1[num].set_rating(temp2);
          }else{
              std::cout<<"Invalid Input.Please Try Again."<<std::endl;
               goto rating;
          }
   times_watched:
            std::cout<<"Number of times you have watched "<<list1[num].get_name()<<":";
            std::cin>>temp2;
          if(times_watched_check(temp2)){
              if(temp2==0){
                   std::cout<<"It means,You haven't Watched the movie.\nSo please first watch the movie."<<std::endl;
                   list1.erase(list1.begin()+num);
                   goto mv;
              }else{
                  list1[num].set_times_watched(temp2);
              }
          }else{
              std::cout<<"Invalid Input.Please Try Again."<<std::endl;
               goto times_watched;
          }
          num++;
          std::cout<<"you want to add more movies:";
          std::cin>>temp;
          if((temp=="Yes")||(temp=="yes")||(temp=="YES")||(temp=="y")||(temp=="Y")){
               list1.resize(list1.size()+1);
                continue;
            }
    }
    if(flag==1){
           mv:
              std::cout<<"do you want to add any other movie:";
              std::cin>>temp;
              if((temp=="Yes")||(temp=="yes")||(temp=="YES")||(temp=="y")||(temp=="Y")){
                   list1.resize(list1.size()+1);
                   flag=0;
                    continue;
                }else{
                    if((temp=="no")||(temp=="NO")||(temp=="n")){
                        break;
                    }else{
                        std::cout<<"Invalid Input.Please Try Again."<<std::endl;
                        goto mv;
                    }
                }
    }

   }
   display(list1);
   return 0;
  }

它不會從這一行編譯,因為當您只能在單個元素上調用 function 時,您正在對電影數組調用 function。

movies.cpp:53:16: error: ‘const class std::vector<movies>’ has no 
member named ‘get_name’  std::cout<<v.get_name();
                                      ^~~~~~~~~~
                              ----->v[I].get_name(); **would compile

也許您的程序的目標是擁有一個名為 Movies 的 Class,其中包含一個 Movies 向量。 否則,命名此 class 電影(單數)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM