C++ 具有特定长度的向量类

C++ 具有特定长度的向量类,c++,class,vector,types,C++,Class,Vector,Types,我试图从STL向量创建特定长度的向量的de类。 这是我的密码 #include <cmath> #include <iostream> #include <cstdio> #include <cassert> #include <vector> #include<complex> using namespace std; template <class T, int N> class KN : public

我试图从STL向量创建特定长度的向量的de类。 这是我的密码

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include<complex>


using namespace std;

template <class T, int N>
class KN : public vector<T> {

public:
KN(){T b=0;
  for (int i=0; i<N; i++){(*this).push_back(b);}}

KN(vector<T> a) { for (int i=0; i<N; i++){(*this).push_back(a[i]);}}     

KN & operator +(const KN & v){     
vector<T> sortie;
for(int i=0; i<N; i++)
    {(sortie).push_back((*this)[i]+v[i]);}   
return KN(sortie);  };

friend int conj(const int& x)
{
   return(x);
 };

friend double conj(const double& x)
{
   return(x);
};

T & operator , (const KN & v){
T c();    
for(int i=0; i<N; i++)
{
    c=c+ (*this)[i] * conj(v[i]);
}    
return c;
};

KN & operator * (const T& e){

vector<T> sortie;     
for(int i=0; i<N; i++)
{       
  sortie.pusk_back((*this)[i]* e);     
}    
return KN(sortie);

};   

};      
#包括
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
模板
类别KN:公共向量{
公众:
KN(){tb=0;
对于(int i=0;i
很多东西。首先,你返回了错误的东西;你没有从+返回引用,你返回了一个对象。
1+1
给你一个
int
,而不是
int&
。其次,当你想返回
KN
时,你正在创建一个
vector
。只要首先创建一个
KN
,你的返回属性Ment不是类型转换,它是从向量创建一个新对象。这里浪费了太多的操作

KN<T, N> operator +(const KN<T, N> & v) {     
    KN<T, N> result(*this);
    for(int i=0; i<N; i++) {
        result[i] += v[i];
    }   
    return result;
};
KN运算符+(常数KN&v){
KN结果(*此);

对于(inti=0;ii)如果可能的话,如果有人告诉我这个函数KN&operator+(const KN&v){vector&sortie;for,我会很高兴的(int i=0;IOH!非常感谢您的解释。我2个月前开始使用C++,我知道的东西很少。现在我了解更多。再次感谢您,我解决了我的代码的所有问题。再次感谢!)
main3.cpp: In function `int main()':
main3.cpp:50: error: name lookup of `i' changed for new ISO `for' scoping
main3.cpp:48: error:   using obsolete binding at `i'
main3.cpp:50: error: invalid types `int[int]' for array subscript

ex3.hpp: In member function `KN<T, N>& KN<T, N>::operator+(const KN<T, N>&) [with T = int, int N = 10]':
main3.cpp:49:   instantiated from here
ex3.hpp:23: error: invalid initialization of non-const reference of type 'KN<int, 10>&' from a temporary of type 'KN<int, 10>'

ex3.hpp: In member function `T& KN<T, N>::operator,(const KN<T, N>&) [with T = int, int N = 10]':
main3.cpp:50:   instantiated from here
ex3.hpp:39: error: pointer to a function used in arithmetic
main3.cpp:50:   instantiated from here
ex3.hpp:39: error: assignment of function `T c() [with T = int, int N = 10]'
ex3.hpp:39: error: cannot convert `int (*)()' to `int ()()' in assignment
main3.cpp:50:   instantiated from here
ex3.hpp:41: error: invalid initialization of non-const reference of type 'int&' from a temporary of type 'int (*)()'
ex3.hpp: In member function `KN<T, N>& KN<T, N>::operator*(const T&) [with T = int, int N = 10]':
main3.cpp:51:   instantiated from here
ex3.hpp:49: error: 'class std::vector<int, std::allocator<int> >' has no member named 'pusk_back'
main3.cpp:51:   instantiated from here
ex3.hpp:51: error: invalid initialization of non-const reference of type 'KN<int, 10>&' from a temporary of type 'KN<int, 10>'
for(int i =0; i<10; i++)
cout<<(vint +vint2)[i]<<endl;
cout<<(vint ,vint2)[i]<<endl;
cout<<(vint*2)[i]<<endl;
KN(vector<T> a)
KN(const vector<T>& a)
KN & operator +(const KN & v) {     
    vector<T> sortie;
    for(int i=0; i<N; i++) {
        (sortie).push_back((*this)[i]+v[i]);
    }   
    return KN(sortie);
};
KN<T, N> operator +(const KN<T, N> & v) {     
    KN<T, N> result(*this);
    for(int i=0; i<N; i++) {
        result[i] += v[i];
    }   
    return result;
};