我在这段代码中遇到了很多问题,我已经解决了大部分问题,但我不知道这里有什么问题。
1>------ Build started: Project: molar mass, Configuration: Debug Win32 ------
1> Source.cpp
1>c:\users\heliz_000\documents\visual studio 2013\projects\molar mass\molar mass\source.cpp(54): error C3867: 'std::basic_ifstream<char,std::char_traits<char>>::close': function call missing argument list; use '&std::basic_ifstream<char,std::char_traits<char>>::close' to create a pointer to member
1>c:\users\heliz_000\documents\visual studio 2013\projects\molar mass\molar mass\source.cpp(55): error C3867: 'std::basic_ofstream<char,std::char_traits<char>>::close': function call missing argument list; use '&std::basic_ofstream<char,std::char_traits<char>>::close' to create a pointer to member
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
这是visual studio 2013的构建输出
#include <iostream>
#include <iomanip>
#include <fstream>
#include <Windows.h>
using std::basic_ofstream;//Thought it would help
using namespace std;
using std::basic_ifstream;//Thought it would help
using std::char_traits;//Thought it would help
/**
* Author: Alex M.
* Date: 3/12/2015
* Desc: Calculates the molar
* mass of compounds,
* elements and
* substances.
*/
void nor();
void tut();
void calc();
int i;
double m;
ifstream inFile;//Is this how to do if/ofstream?
ofstream outFile;
int main()
{
char ele;
bool a;
a = true;
if (i<1)
tut();
nor();
while (a = true)
{
cout << "Enter your compound or type 'Help': ";
cin >> ele;
calc();
while (ele != '\n')//This part is unfinished but seems to work
{
}
}
system("pause");
}
void nor()//This is where i think the problem is
{
inFile.open("runs.dat");
outFile.open("runs.dat");
inFile >> i;
i++;
outFile << i;
inFile.close;
outFile.close;
}
void tut()
{
cout << "Enter your equation with each " << endl << "element seperated by a space." << endl;
cout << "Example: HCl -> H Cl" << endl;
cout << "If theres more than one ion of each " << endl << "element per equation, enter that " << endl << "ion as many time as it appears." << endl;
cout << "Example: NaSO4 -> Na S O O O O" << endl;
system("pause");
}
我已经遍布各个论坛,但似乎没有其他人遇到我的问题。 我在整个代码中评论了我认为问题所在。
我对C很新,所以请不要判断我的简单编码技巧。
答案 0 :(得分:3)
在错误消息中显示:
source.cpp(54)
source.cpp(55)
这意味着这些错误分别在第54行和第55行。你会发现那些行是:
inFile.close;
outFile.close;
您可能打算调用这些函数:
inFile.close();
outFile.close();
如果你没有调用函数(并且函数调用需要括号),那么以这种方式提及成员函数的名称是错误的。
但是,更好的设计是使inFile
和outFile
成为您使用它们的函数内的本地对象。然后,当这些对象超出范围时,文件将自动关闭。