重新定义,字符串的不同基本类型
redefinition , different basic types for string
loadFromTextFile.h
#include <string>
#include <vector>
#include <iostream>
#include <fstream>
#include <cstdlib>
class loadFromTextFile{
private:
int rows = 0;
int columns = 0;
std::string file_path;
std::vector<std::vector<std::string> > nodeGrid;
void process(std::string);
public:
loadFromTextFile(std::string);
loadFromTextFile();
};
从文本文件加载.cpp
#include "loadFromTextFile.h"
using namespace std;
//implementions
loadFromTextFile::loadFromTextFile(string filePath){
file_path = filePath;
string line;
ifstream f(file_path);
if (!f.is_open())
perror("error while opening file");
while (getline(f, line)) {
process(line);
}
if (f.bad())
perror("error while reading file");
cout << "total rows" << rows;
}
void loadFromTextFile::process(string s){
rows++;
cout << s<<endl;
}
main.cpp
#include "loadFromTextFile.h"
int main(){
std::string path = "E:\10x10.txt";
loadFromTextFile(path);
//loadFromTextFile("E:\10x10.txt");//works
}
任何人都知道我为什么要重新定义"路径";字符串路径有不同的基本类型,但是当我直接传递字符串而不是使用变量路径时,它就起作用了。
loadFromTextFile(path);
等价于loadFromTextFile path;
,一个声明。
loadFromTextFile("E:\10x10.txt");
不能被解释为一个声明,因此它被视为一个转换表达式,创建和销毁一个临时对象。它可以编译,但可能也不是您想要的。
您可能想要声明一个命名变量,这样它加载的数据在之后仍然可用:
loadFromTextFile loaded(path);
相关文章:
- 错误:请求从"常量字符 [5]"转换为非标量类型"字符串"
- 类型字符串::长度()。我得到了"Unable to resolve identifier length"
- 如何掩盖对象数组的数据类型字符串到C 中的INT
- 解析参数值 - 字符串和链接值(类型字符串的链接值)与(特定类型的)变量
- C 中的枚举类型字符串类别
- 与基本面作斗争.特别是字符[],字符*和从数组中读取.也许我应该使用类型字符串
- 矢量类型 字符串,但当用整数初始化时
- 如何从const char *类型字符串中删除新线字符
- 在 c++ 中将 ostream 分配给数据类型字符串
- Linux 编译错误 - 无法将类型"字符串"转换为类型"字符串&&"
- LPCSTR 类型字符串出现乱码或显示错误
- C 表达式必须具有类型 - 字符串到const char*
- 将值分配给字符类型字符串
- 将组合框中的类型字符串与其项目进行比较
- 如何将返回类型字符串的数据复制到C++中
- 如何处理csv文件输入流,我需要将文件的每一行中的数据划分为6个不同的变量(类型字符串和int)
- 自定义C类型字符串类的问题
- 在c++中创建自定义C类型字符串类
- 在读取.csv时将数组类型字符串转换为double
- C++将类型字符串变量中的单词转换为小写