小编use*_*632的帖子

c ++:ifstream打开传递文本文件名字符串的问题

我正在尝试将字符串从main传递给另一个函数.此字符串是需要加密的文本文件的名称.据我所知,我正在传递字符串,但是当我尝试使用ifstream.open(textFileName)它时,它并没有完全奏效.但是当我手动硬编码时ifstream.open("foo.txt"),它工作得很好.我需要多次使用此函数,所以我希望能够传入一个文本文件名字符串..

这是我的主要内容

#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif

#ifndef DATAREADER_H
#define DATAREADER_H
#include "DataReader.h"
#endif

using namespace std;

int main()
{
 vector<Data*> database = DataReader("foo.txt");

 return 0; 
}
Run Code Online (Sandbox Code Playgroud)

DataReader的标头

#include <fstream>
#include <iostream>
#include <vector>
#include <string>

#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif

using namespace std;

vector<Data*> DataReader(string textFile);
Run Code Online (Sandbox Code Playgroud)

最后是DataReader.cpp

#include "DataReader.h"

using namespace std;

vector<Data*> DataReader(string textFile)
{
 ifstream aStream;     
 aStream.open(textFile); //line 11
Run Code Online (Sandbox Code Playgroud)

我查找了ifstream.open(),它接受一个字符串和一个模式作为参数.不确定如何处理这些模式,但我尝试了它们但是它们给出了相同的错误信息

DataReader.cpp: In function 'std::vector<Data*, std::allocator<Data*> > DataReader(std::string)':
DataReader.cpp:11: error: …
Run Code Online (Sandbox Code Playgroud)

c++ string parameters text-files

13
推荐指数
1
解决办法
6万
查看次数

"'''令牌之前的预期构造函数,析构函数或类型转换"

我遇到了语法/解析错误,但我似乎找不到它.

DataReader.h:11:错误:在'<'标记之前的预期构造函数,析构函数或类型转换

这是DataReader.h:

#include <fstream>
#include <iostream>
#include <vector>

#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif

vector<Data*> DataReader();   // This is line 11, where the error is..
Run Code Online (Sandbox Code Playgroud)

这是.cpp文件:

#include "DataReader.h"

using namespace std;

vector<Data*> DataReader()
{
 .....
}
Run Code Online (Sandbox Code Playgroud)

我跳过了DataReader()的内容,因为我认为它无关紧要,但我可以根据需要发布它.

感谢您的任何意见/建议.

c++ constructor destructor

4
推荐指数
1
解决办法
6321
查看次数

未在此范围内声明

我收到错误信息

DataReader.h:13: error: 'String' was not declared in this scope
DataReader.cpp:5: error: redefinition of 'std::vector<Data*, std::allocator<Data*> > DataReader'
DataReader.h:13: error: 'std::vector<Data*, std::allocator<Data*> > DataReader' previously declared here
DataReader.cpp:5: error: 'String' was not declared in this scope
Run Code Online (Sandbox Code Playgroud)

这是我的cpp文件

#include "DataReader.h"

using namespace std;

vector<Data*> DataReader(String textFile) //line 5 that's giving error
{........}
Run Code Online (Sandbox Code Playgroud)

这是我的头文件

#include <fstream>
#include <iostream>
#include <vector>
#include <string>

#ifndef DATA_H
#define DATA_H
#include "Data.h"
#endif

std::vector<Data*> DataReader(String something);
Run Code Online (Sandbox Code Playgroud)

当我取出字符串参数并硬编码字符串的名称时,它们工作正常.但我需要多次使用此函数,并希望能够传入一个字符串作为参数.我传递的字符串是文本文件的名称.我错了吗?我似乎无法弄清楚..我的意思是什么意思'字符串'没有在这个范围内声明?我通过它,我包括在内.我的参数有问题吗?如果你能对这件事情有所了解,我们将不胜感激.

院长

c++ scope

2
推荐指数
1
解决办法
3万
查看次数

标签 统计

c++ ×3

constructor ×1

destructor ×1

parameters ×1

scope ×1

string ×1

text-files ×1