如何在 class return 向量中创建一个函数?
How to make a function inside a class return a vector?
我有一个 class,我希望其中一个 class 函数接受一个向量,return 接受一个不同的向量。我试过类似的方法,但我在函数声明和定义中遇到错误,说它们不匹配。
错误信息:
example.cpp:11:18: error: prototype for ‘std::vector<int> myClass::myFunction(std::vector<double>&)’ does not match any in class ‘myClass’
std::vector<int> myClass::myFunction(std::vector<dataType> & myVector){
^
example.cpp:8:22: error: candidate is: std::vector<int> myClass::myFunction(const std::vector<double>&)
std::vector<int> myFunction(const std::vector<dataType> & );
实际代码:
#include<vector>
#include<iostream>
typedef double dataType;
class myClass{
public:
std::vector<int> myFunction(const std::vector<dataType> & );
};
std::vector<int> myClass::myFunction(std::vector<dataType> & myVector){
std::vector<int> results;
results.resize(myVector.size());
for(int i=0; i<results.size(); ++i){
results[i] = 0;
}
return results;
}
int main(){
return 0;
}
根据错误信息,函数是
SetParallel::checkElements(std::vector<double>&)
原型是
SetParallel::checkElements(const std::vector<double>&)
(在两种情况下都省略了初始 const std::vector<int>&
)
你能看出区别吗?
我有一个 class,我希望其中一个 class 函数接受一个向量,return 接受一个不同的向量。我试过类似的方法,但我在函数声明和定义中遇到错误,说它们不匹配。
错误信息:
example.cpp:11:18: error: prototype for ‘std::vector<int> myClass::myFunction(std::vector<double>&)’ does not match any in class ‘myClass’
std::vector<int> myClass::myFunction(std::vector<dataType> & myVector){
^
example.cpp:8:22: error: candidate is: std::vector<int> myClass::myFunction(const std::vector<double>&)
std::vector<int> myFunction(const std::vector<dataType> & );
实际代码:
#include<vector>
#include<iostream>
typedef double dataType;
class myClass{
public:
std::vector<int> myFunction(const std::vector<dataType> & );
};
std::vector<int> myClass::myFunction(std::vector<dataType> & myVector){
std::vector<int> results;
results.resize(myVector.size());
for(int i=0; i<results.size(); ++i){
results[i] = 0;
}
return results;
}
int main(){
return 0;
}
根据错误信息,函数是
SetParallel::checkElements(std::vector<double>&)
原型是
SetParallel::checkElements(const std::vector<double>&)
(在两种情况下都省略了初始 const std::vector<int>&
)
你能看出区别吗?