[英].cpp file will not link with .h file
I am trying to connect two cpp files - MIPSConversion.cpp
and TestMIPSConversion.cpp
- to the same header file - MIPSConversion.h
. 我正在尝试将两个cpp文件
MIPSConversion.cpp
和TestMIPSConversion.cpp
连接到同一头文件MIPSConversion.h
。 However, when I try to call the method readInTheFile()
(located in MIPSConversion.cpp
) from main()
(located in TestMIPSConversion.cpp
) I get the error use of undeclared identifier 'readInTheFile'
. 但是,当我尝试从
main()
(位于TestMIPSConversion.cpp
)调用方法readInTheFile()
(位于MIPSConversion.cpp
)时,我得到use of undeclared identifier 'readInTheFile'
的错误use of undeclared identifier 'readInTheFile'
。 How do I correct this issue? 我该如何解决这个问题?
MIPSConversion.h MIPSConversion.h
#include <iostream>
using namespace std;
class MIPSConversion
{
public:
MIPSConversion();
~MIPSConversion();
void readInTheFile();
};
MIPSConversion.cpp MIPSConversion.cpp
#include <iostream>
#include <fstream>
#include "MIPSConversion.h"
using namespace std;
MIPSConversion::MIPSConversion(){}
MIPSConversion::~MIPSConversion(){}
void MIPSConversion::readInTheFile(){
string inputFileName;
char* arrayString = new char[31];
cout << "Enter the name of the file you want to import: " << endl;
inputFileName = cin;
getline(cin, inputFileName);
ifstream inputFile (inputFileName, ifstream::in);
while (inputFile.good()) {
for(int i = 0; i < 31; i++){
arrayString[i] = inputFile.get();
}
cout << arrayString;
if (inputFile.peek() == std::ifstream::traits_type::eof()){
break;
}
}
inputFile.close();
}
TestMIPSConversion.cpp TestMIPSConversion.cpp
#include <iostream>
#include "MIPSConversion.h"
using namespace std;
int main()
{
readInTheFile();
return(0);
}
You need to create an object of MIPSConversion in main. 您需要在main中创建一个MIPSConversion对象。 Then you can use its member function readInTheFile().
然后,您可以使用其成员函数readInTheFile()。 Like the following:
如下所示:
MIPSConversion myObj;
myObj.readInTheFile();
readInTheFile
is a non- static
member of MIPSConversion
, so you must create a MIPSConversion
object on which to invoke readInTheFile
: readInTheFile
是一个非static
成员MIPSConversion
,所以你必须创建一个MIPSConversion
在其上调用对象readInTheFile
:
MIPConversion mipsConversion;
mipsConversion.readInTheFile();
If there's actually no data you want stored in mipsConversion
by readInTheFile
or other functions, you can get rid of the mipsConversion
class altogether, and make readInTheFile
a file-scope (non-member) function. 如果实际上没有要通过
readInTheFile
或其他函数存储在mipsConversion
的数据,则可以完全摆脱mipsConversion
类,并使readInTheFile
成为文件作用域(非成员)函数。 Or, if you have some other use for the MIPSConversion
class but readInTheFile
doesn't need to access its data, you could leave readInTheFile
in MIPSConversion
but make it static
, which means it can be called even without an object instance of MIPSConversion
, as in: 或者,如果您对
MIPSConversion
类有其他用途,但readInTheFile
不需要访问其数据,则可以将readInTheFile
在MIPSConversion
但使其保持static
,这意味着即使没有MIPSConversion
的对象实例也可以调用它。 :
MIPSConversion::readInTheFile();
You've currently declared readInTheFile as a member function of the class MIPSConversion, meaning you need an instance of the class MIPSConversion in order to actually call the function. 当前,您已将readInTheFile声明为MIPSConversion类的成员函数,这意味着您需要MIPSConversion类的实例才能实际调用该函数。 You can solve this in multiple ways.
您可以通过多种方式解决此问题。
declare the function as static. 将该函数声明为静态。 This means that the function is available to be called without an instance of MIPSConversion (which considering the implementation, appears to be what you're going for).
这意味着可以在没有MIPSConversion实例的情况下调用该函数(考虑到实现,这似乎是您想要的)。 Easy to do,
容易做
class MIPSConversion { public: MIPSConversion(); ~MIPSConversion(); static void readInTheFile(); };
And then when you call the function, prefix it with the class name 然后,当您调用该函数时,请在其前面加上类名
MIPSConversion::readInTheFile();
Create an instance of the class. 创建该类的实例。 Just create an instance and then call the function from it.
只需创建一个实例,然后从中调用该函数即可。
MIPSConversion conversionVariable; conversionVariable.readInTheFile();
If you want to skip the class schemantics altogether (maybe it doesn't make sense to have an "instance" of MIPSConversion, just replace class with Namespace and follow the same calling convention as with static functions. 如果要完全跳过类模式(使用MIPSConversion的“实例”可能没有意义,只需将类替换为Namespace并遵循与静态函数相同的调用约定。
namespace MIPSConversion { void readInTheFile(); }
and in TestMIPSConversion.cpp 并在TestMIPSConversion.cpp中
MIPSConversion::readInTheFile();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.