如何在 C++ class 中包含全局库?
How to include global library in C++ class?
我有 main.cpp、一个 class 学生和一个 global.h 图书馆。
我希望global.h的功能随处可用,所以我这样做了。
global.h
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#ifndef GLOBAL_H
#define GLOBAL_H
int min(vector<int> v) {
int min = -99999999;
for (unsigned int i = 0; i < v.size(); i++) {
if (v[i] > min) min = v[i];
}
return min;
}
double average(vector<int> v) {
int sum = 0;
for (unsigned int i = 0; i < v.size(); i++) {
sum += v[i];
}
return (double)sum / v.size();
}
#endif /* GLOBAL_H */
Student.h
#include "global.h"
#ifndef STUDENT_H
#define STUDENT_H
class Student {
private:
string name;
vector<int> grades;
public:
Student();
void setName(string name);
void addGrade(int grade);
int getBestGrade();
double getAverageGrade();
};
#endif /* STUDENT_H */
Student.cpp
#include "Student.h"
Student::Student() {
}
void Student::setName(string name) {
this->name = name;
}
void Student::addGrade(int grade) {
this->grades.push_back(grade);
}
int Student::getBestGrade() {
return min(this->grades);
}
double Student::getAverageGrade() {
return average(this->grades);
}
main.cpp
#include "Student.h"
using namespace std;
int main(int argc, char** argv) {
Student a;
a.setName("John");
a.addGrade(15);
a.addGrade(13);
a.addGrade(20);
cout << a.getAverageGrade() << endl;
cout << a.getBestGrade() << endl;
return 0;
}
我收到这个错误:
min(...) 的多重定义
average(...)
的多重定义
我似乎多次包含 "global.h"。但我不知道在哪里。确实,我使用了两次 include "Student.h"
。但我认为如果我不这样做,class 将无法工作。
请帮我找出如何在 class.
中包含全局库
谢谢
##############################
解决方案
感谢 ,我现在有了解决方案。
global.h 必须有一个 global.cpp。
global.h
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#ifndef GLOBAL_H
#define GLOBAL_H
int min(vector<int> v);
double average(vector<int> v);
#endif /* GLOBAL_H */
global.cpp
#include "global.h"
int min(vector<int> v) {
int min = -99999999;
for (unsigned int i = 0; i < v.size(); i++) {
if (v[i] > min) min = v[i];
}
return min;
}
double average(vector<int> v) {
int sum = 0;
for (unsigned int i = 0; i < v.size(); i++) {
sum += v[i];
}
return (double)sum / v.size();
}
你的问题没有详细说明这一点,但你似乎在 定义 funcX
和 funcY
in global.h
而不仅仅是声明它们。
预处理器将用这些包含文件的逐字内容替换所有 #include
语句。这是递归发生的。因此在预处理之后,编译器看到一个 "A.cpp",其中包含 global.h
的内容以及 funcX
和 funcY
的完整定义。 (global.h
是通过 A.h
间接包含的。)同样的事情发生在 Main.cpp
。
编译后,A.cpp
和 Main.cpp
的目标文件将包含 funcX
和 funcY
的编译定义。当这些目标文件链接在一起以构建最终的可执行文件时,就会发生错误。链接器将看到这些函数的多个定义并将出错。 (如果这些定义实际上相同,则不会 know/check/care。)
解决方法是在global.h
中只声明这些函数,并将它们的定义放在一个单独的.cpp
文件,说 global.cpp
。例如:
在global.h
中:
// declarations only here
int funcX(int x);
int funcY(int x);
在global.cpp
中:
int funcX(int x)
{
return 2 * x;
}
int funcY(int x)
{
return x + 42;
}
简而言之:您违反了所谓的单一定义规则 (ODR)。
我有 main.cpp、一个 class 学生和一个 global.h 图书馆。
我希望global.h的功能随处可用,所以我这样做了。
global.h
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#ifndef GLOBAL_H
#define GLOBAL_H
int min(vector<int> v) {
int min = -99999999;
for (unsigned int i = 0; i < v.size(); i++) {
if (v[i] > min) min = v[i];
}
return min;
}
double average(vector<int> v) {
int sum = 0;
for (unsigned int i = 0; i < v.size(); i++) {
sum += v[i];
}
return (double)sum / v.size();
}
#endif /* GLOBAL_H */
Student.h
#include "global.h"
#ifndef STUDENT_H
#define STUDENT_H
class Student {
private:
string name;
vector<int> grades;
public:
Student();
void setName(string name);
void addGrade(int grade);
int getBestGrade();
double getAverageGrade();
};
#endif /* STUDENT_H */
Student.cpp
#include "Student.h"
Student::Student() {
}
void Student::setName(string name) {
this->name = name;
}
void Student::addGrade(int grade) {
this->grades.push_back(grade);
}
int Student::getBestGrade() {
return min(this->grades);
}
double Student::getAverageGrade() {
return average(this->grades);
}
main.cpp
#include "Student.h"
using namespace std;
int main(int argc, char** argv) {
Student a;
a.setName("John");
a.addGrade(15);
a.addGrade(13);
a.addGrade(20);
cout << a.getAverageGrade() << endl;
cout << a.getBestGrade() << endl;
return 0;
}
我收到这个错误:
min(...) 的多重定义 average(...)
的多重定义我似乎多次包含 "global.h"。但我不知道在哪里。确实,我使用了两次 include "Student.h"
。但我认为如果我不这样做,class 将无法工作。
请帮我找出如何在 class.
中包含全局库谢谢
##############################
解决方案
感谢
global.h 必须有一个 global.cpp。
global.h
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
using namespace std;
#ifndef GLOBAL_H
#define GLOBAL_H
int min(vector<int> v);
double average(vector<int> v);
#endif /* GLOBAL_H */
global.cpp
#include "global.h"
int min(vector<int> v) {
int min = -99999999;
for (unsigned int i = 0; i < v.size(); i++) {
if (v[i] > min) min = v[i];
}
return min;
}
double average(vector<int> v) {
int sum = 0;
for (unsigned int i = 0; i < v.size(); i++) {
sum += v[i];
}
return (double)sum / v.size();
}
你的问题没有详细说明这一点,但你似乎在 定义 funcX
和 funcY
in global.h
而不仅仅是声明它们。
预处理器将用这些包含文件的逐字内容替换所有 #include
语句。这是递归发生的。因此在预处理之后,编译器看到一个 "A.cpp",其中包含 global.h
的内容以及 funcX
和 funcY
的完整定义。 (global.h
是通过 A.h
间接包含的。)同样的事情发生在 Main.cpp
。
编译后,A.cpp
和 Main.cpp
的目标文件将包含 funcX
和 funcY
的编译定义。当这些目标文件链接在一起以构建最终的可执行文件时,就会发生错误。链接器将看到这些函数的多个定义并将出错。 (如果这些定义实际上相同,则不会 know/check/care。)
解决方法是在global.h
中只声明这些函数,并将它们的定义放在一个单独的.cpp
文件,说 global.cpp
。例如:
在global.h
中:
// declarations only here
int funcX(int x);
int funcY(int x);
在global.cpp
中:
int funcX(int x)
{
return 2 * x;
}
int funcY(int x)
{
return x + 42;
}
简而言之:您违反了所谓的单一定义规则 (ODR)。