以向量作为参数的 C++ 调用函数
c++ calling functions with vectors as the parameter
我有以下代码,当我尝试构建它时,我不断收到此错误:
错误:
1>------ Build started: Project: practice, Configuration: Debug Win32 ------
1> pratice.cpp
1>pratice.obj : error LNK2019: unresolved external symbol "double __cdecl CalcScore(class std::vector<double,class std::allocator<double> > *)" (?CalcScore@@YANPAV?$vector@NV?$allocator@N@std@@@std@@@Z) referenced in function _main
1>C:UsersNeil ArmstrongDesktoppracticeDebugpractice.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
代码为:
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
double CalcScore(double);
void points(double);
void grade(char);
int main ()
{
double CalcScore(vector<double> *);
// Declare constant
double first_exam = 0;
double second_exam = 0;
double final_exam = 0;
double aHomework = 0;
double totalPoints = 0;
double median = 0;
double SUM = 0;
// Declare a vector of doubles
vector<double> homework;
// Ask the user to enter in the score for the first midterm.
cout << "nPlease enter in the score for the first exam: ";
// This value is read in and saved.
cin >> first_exam;
// Ask the user to enter in the score for the second midterm.
cout << "nPlease enter in the score for the second exam: ";
// This value is read in and saved.
cin >> second_exam;
// Ask the user to enter in the score for the final exam.
cout << "nPlease enter in the score for the final exam: ";
// This value is read in and saved.
cin >> final_exam;
// Store the homework scores in a vector.
// Uses the push_back function to add each homework score to the vector.
// Then asks the user to enter in the scores for the homework assignments.
// Any number of scores can be entered in.
// When done, the user signals completion by pressing Ctrl-z (the Ctrl key
// and the letter z) followed by the Enter key.
cout << "nEnter the score for a homework assignment(press ctrl-z to quit): ";
cin>>aHomework;
while (!cin.eof()) // No Ctrl-Z
{
if (cin.good()) // The data entry was valid, i.e., an integer
{
homework.push_back(aHomework);
}
else
{
// The failbit was set, so this entry must be invalid.
cout << "Invalid entry!" << endl;
cin.clear(); // clear error flag
cin.sync(); // Re-sync stream
}
// Get next data entry
cout << "nEnter the score for a homework assignment(press ctrl-z to quit): ";
cin>>aHomework;
}
median = CalcScore( &homework);
cout << endl;
system("PAUSE");
return 0;
}// End main
double CalcScore(vector<double> homework)
{
double median = 0;
const int TWO = 2;
size_t size = homework.size();
sort(homework.begin( ), homework.end( ) );
if (size % TWO == 0)
{
median = (homework[size / TWO - 1] + homework[size / TWO]) / TWO;
}
else
{
median = homework[size / TWO];
}
return median;
}
解决此错误的任何帮助都会很棒谢谢尼尔
你声明
double CalcScore(vector<double> *);
但从未实施过。您实现了:
double CalcScore(vector<double> homework)
此外,将函数声明/定义移到main
之外。
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- 为什么使用 "this" 指针调用派生成员函数?
- 函数调用中参数的顺序重要吗
- OpenGL - 在抛出"__gnu_cxx::recursive_init_error"实例后终止调用?
- 基于另一个成员参数将函数调用从类传递给它的一个成员
- 为什么我的C#代码在调用回C++COM直到Task时会暂停.等待/线程.加入
- 在c++类上调用void函数
- 为什么 std::unique 不调用 std::sort?
- 调用专用模板时出错"no matching function for call to [...]"
- 选择要调用的构造函数
- C++为什么尽管我调用了void函数,它却不起作用
- 构造函数正在调用一个使用当前类类型的函数
- 变量没有改变?通过向量的函数调用
- 没有为自己的结构调用列表推回方法
- 调用'begin(int [n])'没有匹配函数
- 什么时候调用析构函数
- 如何用参数值调用函数(仅在运行时已知)
- std::cout.imbue()多重调用
- 函数何时会在c++中包含stack_Unwind_Resume调用