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);
}
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};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.