我在尝试从文本文件中读取输入时遇到问题,最初程序通过cin从控制台获取输入,但需要将其更改为从文本文件中读取输入。如果它不依赖多个类来获取输入,那似乎很容易。我尝试过创建一个包含以下内容的globals.h文件:
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <string>
#include <fstream>
extern std::ifstream input;
然后是globals.cpp
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <string>
#include <fstream>
std::fstream input("C:\\Users\\ricky\\CLionProjects\\module3\\input.txt");
这里是我的main.cpp文件,我相信它可以在这个文件中运行,但是因为我有其他类需要读取输入,我收到链接错误
#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <string>
#include <fstream>
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "symboltable.h"
#include "parse.h"
#include "globals.h"
SymbolTable symbolTable;
void parseAssignments();
int main()
{
input.open("C:\\Users\\ricky\\CLionProjects\\module3\\input.txt");
for( std::string line; getline( input, line ); )
{
cout << "\n";
cout << "Working on line: " << line << endl;
Expression* expression;
char paren, comma;
cout << "Read in expression: ";
input >> paren;
expression = SubExpression::parse();
input >> comma;
parseAssignments();
cout << "Value = " << expression->evaluate() << endl;
}
return 0;
}
下面的operand.cpp ......
#include <cctype>
#include <iostream>
#include <list>
#include <string>
using namespace std;
#include "expression.h"
#include "subexpression.h"
#include "operand.h"
#include "variable.h"
#include "literal.h"
#include "parse.h"
#include "globals.h"
#include <fstream>
Expression* Operand::parse()
{
char paren;
double value;
input >> ws;
if (isdigit( input.peek()))
{
input >> value;
Expression* literal = new Literal(value);
return literal;
}
if ( input.peek() == '(')
{
input >> paren;
return SubExpression::parse();
}
else
return new Variable(parseName());
return 0;
}
这是我收到的错误:
LINK Pass 1: command "C:\PROGRA~2\MICROS~3\2017\COMMUN~1\VC\Tools\MSVC\1413~1.261\bin\Hostx86\x86\link.exe /nologo @CMakeFiles\module3.dir\objects1.rsp /out:module3.exe /implib:module3.lib /pdb:C:\Users\ricky\CLionProjects\module3\cmake-build-debug\module3.pdb /version:0.0 /machine:X86 /debug /INCREMENTAL /subsystem:console kernel32.lib user32.lib gdi32.lib winspool.lib shell32.lib ole32.lib oleaut32.lib uuid.lib comdlg32.lib advapi32.lib /MANIFEST /MANIFESTFILE:CMakeFiles\module3.dir/intermediate.manifest CMakeFiles\module3.dir/manifest.res" failed (exit code 1120) with the following output:
main.cpp.obj : error LNK2001: unresolved external symbol "class std::basic_ifstream<char,struct std::char_traits<char> > input" (?input@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A)
Operand.cpp.obj : error LNK2001: unresolved external symbol "class std::basic_ifstream<char,struct std::char_traits<char> > input" (?input@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A)
Parse.cpp.obj : error LNK2001: unresolved external symbol "class std::basic_ifstream<char,struct std::char_traits<char> > input" (?input@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A)
Subexpression.cpp.obj : error LNK2001: unresolved external symbol "class std::basic_ifstream<char,struct std::char_traits<char> > input" (?input@@3V?$basic_ifstream@DU?$char_traits@D@std@@@std@@A)
module3.exe : fatal error LNK1120: 1 unresolved externals
NMAKE : fatal error U1077: '"C:\Program Files\JetBrains\CLion 2018.1.2\bin\cmake\bin\cmake.exe"' : return code '0xffffffff'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.
NMAKE : fatal error U1077: '"C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Tools\MSVC\14.13.26128\bin\HostX86\x86\nmake.exe"' : return code '0x2'
Stop.