对螺柱平均值的未定义引用

undefined reference to studAverage

本文关键字:未定义 引用 平均值      更新时间:2023-10-16

我在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 C2065Error 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>