I have a problem with my C++ code. I need to make the sum of the vectors component by component. For example, if I have A(2,1) and B(3,3) the result should be (5,4). I tried to do something but apparently I have a problem and I don't know what to do. The error is: ,,No member called push in std::_1vector" My code is:
#include <iostream>
#include "stack_base.h"
#include <vector>
using namespace std;
template<typename T>
class App {
public:
Stack<T> * stack;
App(Stack<T> &stack) {
this->stack = &stack;
}
T sum(){
Stack<T> *tempStack = stack;
T sum=0;
int size = stack->getTopLevel();
for(int i=0; i<=size; i++) {
sum+=tempStack->peek();
tempStack->pop();
}
return sum;
}
T substract(){
Stack<T> tempStack = *stack;
T substr=0;
for(int i=0; i<=stack->getTopLevel(); i++) {
substr-=tempStack.peek();
tempStack.pop();
}
return substr;
}
};
int main(){
Stack<int> myStack;
App<int> a(myStack);
int values[7] = {5, 2, 3, 1, 4, 8, 6};
int values1[7] = {5, 2, 3, 1, 4, 8, 6};
unsigned int i = 0;
vector <int> v1;
vector <int> v2;
vector <int> v3;
cout << "Filling the Numbers\n";
for (i=5;i < 125 ; i = i + 5) {
myStack.push(v1[i/10]);
myStack.push(v2[i/100]);
myStack.push(v3[i]);
v3[i]=v1[i]+v2[i]; //here it's the problem
}
cout << "Adding the numbers\n";
for (i = 0; i < v1.size(); i++) {
v3[i] = v1[i] + v2[i];
}
cout << "Printing the numbers\n";
for (i = 0; i < v3.size() ; i++) {
cout << v3[i];
}
return 0;
}
Aucun commentaire:
Enregistrer un commentaire