1
votes

I have the following problem. I´m just trying to output a vector but it says "invalid operands to binary expression ('std::ostream' (aka 'basic_ostream') and 'const std::vector')"

Code:

#include <stdio.h>
#include <iostream>
#include <vector>




template <size_t B, size_t N, typename T = int>
class Number
{

  private:
   std::vector<T> v[N]; // Vector que contendrá el numero desglozado
   void toBase(int valor);

 public:
   Number(int valor); // Constructor
   Number <B,N,T> suma(const Number<B,N,T>& sumando) const;
   std::ostream& write(std::ostream& os)const;
 };


template <size_t B, size_t N, typename T>
Number<B,N,T>::Number(int valor)
{
  toBase(valor);
  std::cout << write() << std::endl;
}



template <size_t B, size_t N, typename T>
void Number<B,N,T>::toBase(int valor)
{
   for(int i = 0; i < N; i++) {
    int aux = valor % B;
    v[i].push_back(aux);
  }
}



template <size_t B, size_t N, typename T>
std::ostream& Number<B,N,T>::write(std::ostream& os) const
{
  for(int i = 0; i < N; i++){
    os << v[i] << std::endl;
   }
}

I thought the problem was that i was trying to output the vector with a simple for loop. So i tried to output it with an ostream but i have the same error. So the ostream its ok i think. Just the outputting vector is the problem and i dont know why. Any ideas? Thanks you!

main:

#include <stdio.h>
#include <iostream>

#include "number.hpp"


int main() {

Number<2,10> N1(100);
Number<2,10> N2(200);

}
1
v is an array of vectors, for starters. - Shawn
v is an array of vectors, for starters. Vectors don't come with an operator<<() ... - Shawn
You also have a problem with std::cout << write() and with the fact that write is a non-void function that doesn't return anything. - molbdnilo
It's the same array/vector confusion again. If you really think you need an array of vectors then you are going to have to write two loops, an outer loop to go through the array and an inner loop to go through the vector. - john
I know what a vector is. I dont get your point. - Michalistico

1 Answers

1
votes

It seems you confused with braces.

std::vector<T> v[N];

You declaration above declares the array of vectors. I think you want to declare a vector with 4 elements/digits. In this case you have to use other braces:

std::vector<T> v{N};