我有一个班级作业说:
为接受两个 int 参数并返回一个 int 的函数编写声明,并声明其元素具有此函数指针类型的向量。
由于函数和向量都是 int 这是正确的吗?我对指针仍然很模糊。这就是我所拥有的:
#include <iostream>
#include <vector>
using std::cin; using std::cout; using std::endl; using std::vector;
// This is my function that take two ints and returns and int
int AddFunc ( int a, int b) { int addResult; addResult = a + b; return (addResult);}
int main()
{
vector <int> v1; // Declare a vector whose elements have this functions pointer types
int add1, add2, add3 = 0;
cout << "Enter two numbers to be added with my AddFunc function: ";
cin >> add1 >> add2;
add3 = AddFunc (add1, add2);
cout << "The numbers added equal: " << add3 << endl;
v1.push_back(add3);
cout << "The first element in the vector v1 is: " << v1 [0] << endl;
return 0;
}