This question may get thrown out but I've been searching and searching for help on this. I am a total beginner.. OK: I have a template class with a function that adds two vectors and outputs to a 3rd vector. I need to call if from my main program. Here is my template (very simple).
#include <vector>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
template<class T>
class polyClass {
public:
//position 0 always constant, pos1 x^1, pos2 x^2
vector<T> a;
vector<T> b;
vector<T> result;
int addVectors(T& a, T& b, T& result) {
for (vector<T> i = a[i].begin; i != a[i].end(); i++) {
result[i] = a[i] + b[i];
return result;
}
}
};
I need to call the addVectors function from my main program. And I keep getting the message that I did not declare 'a', 'b', and 'result' in this scope.I am going on 3 hours with this and could really use some help. Here is my main program.
#include <iostream>
#include<vector>
using namespace std;
#include "PolynomMult_Add.h"
int main() {
polyClass<int> newPolyClass;
newPolyClass.a = {3, 4, 2};
newPolyClass.b = {3, 3, 1};
newPolyClass.result = {};
newPolyClass.addVectors(a, b, result);
return 0;
}
You were getting undeclared variables because you need to use newPolyClass.a, newPolyClass.b, newPolyClass.result instead of a, b and resullt. Here is corrected code.
#include <vector>
#include<iostream>
#include<iomanip>
#include<algorithm>
using namespace std;
template<class T>
class polyClass {
public:
//position 0 always constant, pos1 x^1, pos2 x^2
vector<T> a;
vector<T> b;
vector<T> result;
vector<T> addVectors(vector<T> & a, vector<T> & b, vector <T> & result) {
for (auto i = a.begin(), j = b.begin(); i != a.end() && j != b.end(); i++, j++) {
result.push_back (*i + *j);
}
return result;
}
};
int main() {
polyClass<int> newPolyClass;
newPolyClass.a = {3, 4, 2};
newPolyClass.b = {3, 3, 1};
newPolyClass.result = {};
newPolyClass.addVectors(newPolyClass.a, newPolyClass.b, newPolyClass.result);
return 0;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.