gpt4 book ai didi

c++ - 具有特定长度的 vector 类

转载 作者:行者123 更新时间:2023-11-28 07:17:07 25 4
gpt4 key购买 nike

我正在尝试从 STL vector 中创建特定长度的 vector 类。这是我的代码

#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);

};

};

我正在用这个 main.cpp 测试我的函数

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cassert>
#include <vector>
#include<complex>
#include "ex3.hpp"

using namespace std;

int main()
{
KN<int,10> vint;
KN<int,10> vint2;

for(int i =0; i<10; i++)
{vint[i]=1;}

for(int i =0; i<10; i++)
{vint2[i]=2;}



for(int i =0; i<10; i++)
cout<<(vint +vint2)[i]<<endl;
cout<<(vint ,vint2)[i]<<endl;
cout<<(vint*2)[i]<<endl;

但我有所有这些错误:

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)

这不是声明从 vector 复制构造函数的方式。你应该使用:

KN(const vector<T>& a)

拜托,拜托,除了重载 operator, 之外,请找到其他方法来做到这一点.您甚至没有做对:您需要返回一个临时对象,而不是就地修改对象并返回对它的引用。

您的代码也充满了拼写错误。请清理它并更好地解释您在这里要做什么。


If it was posible i would be happy if anybody tell me what's wrong with this function

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);
};

很多东西。首先,你退回了错误的东西;你不从 + 返回一个引用,你返回一个对象。 1 + 1给你一个int ,不是 int& .其次,您正在创建一个 vector<T>当你想返回 KN 时.只需创建一个 KN首先!您的 return 语句不是类型转换,它是从 vector 创建一个新对象。这里有很多浪费的操作。

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;
};

最后,如果你想要一个固定长度的 vector ,你应该使用 std::array来自 C++11 或 Boost::array 的模板如果您没有 C++11,请使用 Boost。

关于c++ - 具有特定长度的 vector 类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20055188/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com