C++ 使用类和头的两个错误
我得到了错误类的重新定义 此外,错误ID不是类进程的成员 ID前缺少分号 我尝试了char*而不是string。。也不工作 有什么帮助吗?我好像错过了什么 提前谢谢 process.h文件C++ 使用类和头的两个错误,c++,C++,我得到了错误类的重新定义 此外,错误ID不是类进程的成员 ID前缺少分号 我尝试了char*而不是string。。也不工作 有什么帮助吗?我好像错过了什么 提前谢谢 process.h文件 #ifndef PROCESS_H #define PROCESS_H class process { public: string ID; int run_time; int arrival_time; process(); int get_start_time(); int get
#ifndef PROCESS_H
#define PROCESS_H
class process
{
public:
string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
process.cpp文件
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
class process
{
public:
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
};
#包括
#包括
#包括
#包括
#包括“process.h”
使用名称空间std;
类进程
{
公众:
process::process(){};
int进程::获取开始时间()
{
int x;
返回x;
}
int进程::获取完成时间(int开始、int运行)
{
int x;
返回x=开始+运行;
}
int进程::获取时间(int完成,int开始)
{
int x;
返回x=完成-开始;
}
浮动过程::获取加权时间()
{};
};
问题1重新开始上课在
cpp
文件中,您不需要编写类进程
。应该是
#include <cstdlib>
#include <stdio.h>
#include <string>
#include <sstream>
#include "process.h"
using namespace std;
process:: process() {};
int process:: get_start_time()
{
int x;
return x;
}
int process:: get_finish_time(int start, int run)
{
int x;
return x= start +run;
}
int process:: get_TA_time(int finish, int start)
{
int x;
return x= finish - start;
}
float process:: get_weighted_TA_time()
{};
#包括
#包括
#包括
#包括
#包括“process.h”
使用名称空间std;
process::process(){};
int进程::获取开始时间()
{
int x;
返回x;
}
int进程::获取完成时间(int开始、int运行)
{
int x;
返回x=开始+运行;
}
int进程::获取时间(int完成,int开始)
{
int x;
返回x=完成-开始;
}
浮动过程::获取加权时间()
{};
在头文件中,您已经给出了所需的声明。在cpp
中,您需要给出定义。但是,由于您现在在类范围之外,因此必须提供您已经在执行的成员的完全限定名。(例如:intprocess::
get\u finish\u time(int start,int run))
如果您再次使用类进程
,编译器将不会提示您打算使用同一个类,并且不希望使用导致类重新定义错误的新类。在C++中,不允许在其他地方部分地完成一个类并完成其余部分。(不能与继承一起使用)
问题2:字符串问题添加
#在头文件中包含,并使用std::string
或使用std::string添加行存在一些问题。其中之一是:
process.h
需要#包括。包含后,在所有字符串变量前面加上std::
#ifndef PROCESS_H
#define PROCESS_H
#include <string>
class process
{
public:
std::string ID;
int run_time;
int arrival_time;
process();
int get_start_time();
int get_finish_time(int start, int run);
int get_TA_time(int finish, int start);
float get_weighted_TA_time();
};
#endif
#ifndef过程
#定义过程
#包括
类进程
{
公众:
std::字符串ID;
int运行时间;
国际到达时间;
过程();
int get_start_time();
int get_finish_time(int start,int run);
int获取时间(int完成,int开始);
float get_weighted_tau_time();
};
#恩迪夫
原始代码的问题在于:
依赖于在包含进程.h
之前包含了一些外部模块
依赖于某些外部模块使用名称空间std声明代码>在包含进程.h
之前
这些都不能保证。因为您在这两个文件中都定义了类,所以它给出了类重新定义
错误。第二,将“string.h”
添加到头文件中。谢谢,这纠正了类重新定义错误!!您知道如何解决字符串ID问题吗?@MaramAlaa在头文件中包含“string.H”