Как правильно вызвать «void print_x (ostream & o) const» в main. cpp - PullRequest
0 голосов
/ 25 февраля 2020

Я пишу свой собственный класс vector.h, и у меня есть собственный определенный метод void print_x (ostream & o) const, который выглядит следующим образом:

void print_x(ostream& o) const {
    o<<"[";

  for(size_t i{0}; i<this->sz; i++){
    if(i<this->size()-1)
    o<< this->values[i] << ", ";
    else 
    o<< this->values[i];
  } 
 o<< "]";

 return o;

 }

Как я могу вызвать эту функцию в моем main? Я пытаюсь вызвать эту функцию в моем основном как:

#include<iostream>
#include<stdexcept>
#include<algorithm>
#include<numeric>
#include<iterator>
#include<set>
#include"vector.h"
using namespace std;

int main() {
  Vector<int> v1{1,2,2,3,3,3,4};
  Vector<int> v2{1,1,3,3,4,2};

  v1.print_x(std::cout);
  v2.print_x(v2);

  return 0;
}

Но это дает мне ошибку вроде:

main.cpp: In function ‘int main()’:
main.cpp:29:14: error: no matching function for call to ‘Vector<int>::print_x(Vector<int>&)’
 v2.print_x(v2);
              ^
In file included from main.cpp:9:0:
vector.h:407:7: note: candidate: void Vector<T>::print_x(std::ostream&) const [with T = int; std::ostream = std::basic_ostream<char>]
  void print_x(ostream& o) const {
       ^
vector.h:407:7: note:   no known conversion for argument 1 from ‘Vector<int>’ to ‘std::ostream& {aka std::basic_ostream<char>&}’
vector.h: In instantiation of ‘void Vector<T>::print_x(std::ostream&) const [with T = int; std::ostream = std::basic_ostream<char>]’:
main.cpp:28:21:   required from here
vector.h:418:9: error: return-statement with a value, in function returning 'void' [-fpermissive]
  return o;

...