对螺柱平均值的未定义引用
undefined reference to studAverage
我在C++中遇到引用问题,我即将计算班级分数的平均值,现在我被错误困住了。我的类在文件名 classAverage.h 中是
#ifndef CLASSAVERAGE_H_INCLUDED
#define CLASSAVERAGE_H_INCLUDED
#include <iostream>
#include <vector>
using namespace std;
class studAverage {
public:
void test();
void sDetails();
double avgMarks(vector<double> testMarks);
// void getStudentWithLowMarks(vector<string> names , vector<double> marks);
// void getStudentWithHighMarks(vector<string> names , vector<double> marks);
private:
string sName;
double sAvg;
double cAvg;
double totalMarks;
};
#endif
我在classAverageimpl.cpp中的类实现代码是
#include <iostream>
#include "classAverage.h"
#include <vector>
using namespace std;
void studAverage :: test(){
cou<<"::"<<endl;
}
void studAverage :: sDetails(){
// sName = name;
//sAvg = testMarks;
cout<<"gggg"<<endl;
cout<<"enter the list"<<endl;
}
double studAverage :: avgMarks(vector<double> testMarks){
int sum=0;
double classAvg;
for(int i=0;i<testMarks.size();i++){
sum+=testMarks[i];
}
classAvg = (sum/testMarks.size())*100;
cout<<"The ave ::"<<classAvg;
return classAvg;
}
我的主程序.cpp是
#include <iostream>
#include <vector>
#include "classAverage.h"
#include <string>
using namespace std;
int main()
{
studAverage stud;
int sCount,i;
string name;
double marks;
vector<double> sMarks;
sMarks.clear();
vector<string> sName(10);
sName.clear();
cout << "Hello world!" << endl;
cout<<"Enter num of students in the class"<<endl;
cin>>sCount;
for(i=0;i<sCount;i++){
cout<<"Enter student details"<<endl;
cin>>name;
cout<<"Enter student marks"<<endl;
cin>>marks;
sName.push_back(name);
sMarks.push_back(marks);
}
cout<<"The size of name vector is "<<sName.size()<<endl;
cout<<"The size of marks vector is "<<sMarks.size()<<endl;
stud.sDetails();
return 0;
}
请帮帮我
在 'classAverageimpl.cpp' 中将#inclede "classAverage.h"
更改为#include "classAverage.h"
。
编辑::
答:undefined reference to studAverage
即将到来,因为你的#inclede
应该#include
B. 单独编译main.cpp
,你会得到Error C2679
,因为你的main.cpp
不包括#include <string>
C. 在编译classAverageimpl.cpp
时,你会得到Error C2065
和Error C4716
,因为你的classAverageimpl.cpp
不包括#include <vector>
和方法double studAverage :: avgMarks(vector<double> testMarks)
期望return value of type double
看逻辑,只是是什么让它无法编译,并且有几个小错误。修复这些问题,程序将运行。它是否会按预期工作是一个不同的问题。
在classAverageImpl中.cpp添加:
#include <vector>
和改变
#inclede "classAverage.h"
自
#include "classAverage.h"
此外,double studAverage::avgMarks(vector<double> testMarks)
还需要有一个返回值,例如:
return classAvg; // add this at the end of the method
在main.cpp中添加:
#include <string>
- 对C宏的未定义引用,但在定义它时会出现重新定义错误
- 编译时的 CImg 库返回对"__imp_SetDIBitsToDevice"的未定义引用
- 对Py_Initialize()的未定义引用
- 使用mysql c++连接器的未定义引用
- 对 Scalar ::Scalar() 的未定义引用
- 对复制 CTOR 和 CTOR 的未定义引用
- 对显式实例化的模板函数的未定义引用
- TensorRT (C++ API) 对"createNvOnnxParser_INTERNAL"的未定义引用
- 2个模板化类的非模板友元函数未定义引用错误
- 编译 libfluid 样本控制器时对"event_base_del_virtual"的未定义引用
- 获取对function_name的未定义引用
- 对 'std::thread::_M_start_thread CMake 的未定义引用进行基准测试
- 对结构方法的未定义引用
- 使用内联函数 c++ 的未定义引用
- 对 CMake 中'cudaRegisterLinkedBinary'链接错误的未定义引用?
- 对 DLOPEN 的未定义引用
- QT C++中对全局变量的未定义引用
- 快速数学导致对"__pow_finite"的未定义引用
- 对 boost::system::d etail::system_category_instance 的未定义引用,从
- OpenCV 3.4.3 中对 'cv::String::d eallocate()' 错误的未定义引用