如何在C++中传递向量作为参数 < >我需要创建一个C++程序,它实现和= NUM1+NUM2,和= NUM1-NUM2,和= NUM1*NUM2,其中,和,NUM1,NUM2是包含存储整数的向量的对象。我需要为此使用运算符重载。这是我提出的代码,但我无法理解这些错误的原因。我将向量声明为'std::vector n;'并尝试将另一个向量作为参数传递给函数。对于栈溢出和C++编程,我都是新手。 #include<iostream> using namespace std; class NUM { private: std::vector<int> n ; public: //function to get number void getNum(std::vector<int> x) { n=x; } //function to display number void dispNum(void) { cout << "Number is: " << n; } //add two objects - Binary Plus(+) Operator Overloading NUM operator +(NUM &obj) { NUM x; //create another object x.n=this->n + obj.n; return (x); //return object } }; int main() { NUM num1,num2,sum; num1.getNum(10); num2.getNum(20); //add two objects sum=num1+num2; sum.dispNum(); cout << endl; return 0; }
这些是我得到的错误如何在C++中传递向量作为参数 < >我需要创建一个C++程序,它实现和= NUM1+NUM2,和= NUM1-NUM2,和= NUM1*NUM2,其中,和,NUM1,NUM2是包含存储整数的向量的对象。我需要为此使用运算符重载。这是我提出的代码,但我无法理解这些错误的原因。我将向量声明为'std::vector n;'并尝试将另一个向量作为参数传递给函数。对于栈溢出和C++编程,我都是新手。 #include<iostream> using namespace std; class NUM { private: std::vector<int> n ; public: //function to get number void getNum(std::vector<int> x) { n=x; } //function to display number void dispNum(void) { cout << "Number is: " << n; } //add two objects - Binary Plus(+) Operator Overloading NUM operator +(NUM &obj) { NUM x; //create another object x.n=this->n + obj.n; return (x); //return object } }; int main() { NUM num1,num2,sum; num1.getNum(10); num2.getNum(20); //add two objects sum=num1+num2; sum.dispNum(); cout << endl; return 0; },c++,C++,这些是我得到的错误 main.cpp:7:14: error: ‘vector’ in namespace ‘std’ does not name a template type std::vector<int> n ; ^~~~~~ main.cpp:11:26: error: ‘std::vector’ has not been declared void getNum(std::vector<int>
main.cpp:7:14: error: ‘vector’ in namespace ‘std’ does not name a template type
std::vector<int> n ;
^~~~~~
main.cpp:11:26: error: ‘std::vector’ has not been declared
void getNum(std::vector<int> x)
^~~~~~
main.cpp:11:32: error: expected ‘,’ or ‘...’ before ‘<’ token
void getNum(std::vector<int> x)
^
main.cpp: In member function ‘void NUM::getNum(int)’:
main.cpp:13:13: error: ‘n’ was not declared in this scope
n=x;
^
main.cpp:13:15: error: ‘x’ was not declared in this scope
n=x;
^
main.cpp: In member function ‘void NUM::dispNum()’:
main.cpp:18:38: error: ‘n’ was not declared in this scope
cout << "Number is: " << n;
^
main.cpp: In member function ‘NUM NUM::operator+(NUM&)’:
main.cpp:24:15: error: ‘class NUM’ has no member named ‘n’
x.n=this->n + obj.n;
^
main.cpp:24:23: error: ‘class NUM’ has no member named ‘n’
x.n=this->n + obj.n;
^
main.cpp:24:31: error: ‘class NUM’ has no member named ‘n’
x.n=this->n + obj.n;
您没有包含导致错误的向量头
#include <iostream>
#include <vector>
using namespace std;
这将解决错误。
或者您可以改为包含标题
#include <bits/stdc++.h>
using namespace std;
这将同时包括整个标准库。您至少缺少了include,这修复了许多错误。getNum看起来很奇怪。我不认为它做了你认为它做的,而且你期望的是一个向量,但传递的是一个数字。我建议你至少要熟悉基本概念。既不直接包含任何内容,也不包含整个标准库都是一个好主意。
#include<iostream>
#include <vector>
#include <algorithm>
using namespace std;
class NUM
{
private:
std::vector<int> n ;
public:
//function to get number
void getNum(std::vector<int> x)
{
n=x;
}
//function to display number
void dispNum(void)
{
cout << "Number is: " <<endl;
for_each(n.begin(),n.end(),[](int i){cout <<i<<endl;});
}
//add two objects - Binary Plus(+) Operator Overloading
NUM operator +(NUM &obj)
{
NUM x; //create another object
//x.n=this->n + obj.n;
if(this->n.size() == obj.n.size())
for (int i = 0; i < this->n.size(); ++i) {
x.n.push_back(this->n.at(i)+obj.n.at(i));
}
return x; //return object
}
};
int main()
{
NUM num1,num2,sum;
num1.getNum({10});
num2.getNum({20});
//add two objects
sum=num1+num2;
sum.dispNum();
cout << endl;
return 0;
}