[英]invalid operands to binary expression ('std::ostream' (aka 'basic_ostream<char>') and 'const std::vector<int>')
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')"
我只是想输出一个向量,但是它说“对二进制表达式无效的操作数('std :: ostream'(又名'basic_ostream')和'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. 我以为问题是我试图用一个简单的for循环输出向量。 So i tried to output it with an ostream but i have the same error.
所以我试图用一个ostream输出它,但是我有同样的错误。 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.
我认为您想声明一个包含4个元素/数字的向量。 In this case you have to use other braces:
在这种情况下,您必须使用其他花括号:
std::vector<T> v{N};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.