我有一個課堂作業,上面寫着:有一個函數指針類型是什麼意思?
寫聲明,它有兩個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;
}
順便說一句,有用的職業建議:不要養成將所有東西寫在一行上的習慣。它使事情變得難以閱讀。 –