使用Visual Studio 2012编译时,此代码会给我错误,但是使用代码块就可以了
This code gives me errors when compiled using visual studio 2012, but with codeblocks it is okay
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <vector>
#include <iterator>
#define ADDR "c:\Users\Library2\Desktop\Books record\"
using namespace std;
int main()
{
ifstream fin(ADDR "reportcard.csv", ios::binary);
string line;
int rowCount=0;
int rowIdx=0;
while(getline(fin,line)){
rowCount++;
}
vector<string> data[**rowCount**];//this rowCount gave me "expression must have a constant value"
fin.clear();
fin.seekg(fin.beg);
while(getline(fin,line))
{
stringstream ss(line);
string value;
while(getline(ss,value,',')){
data[rowIdx].push_back(value);
}
rowIdx++;
}
fin.close();
int colNum;
string colName = "LAST PERSON";
static int it;
for(vector<string>::iterator it = data[0].begin(); it != data[0].end(); ++it)
{
if ((*it)== colName)
{
colNum = distance(data[0].begin(),it);//distance() gave me "no instances of function templates matches argument"
break;
}
}
cout << data[1][colNum] << "t";
return 0;
}
我- 试图弄清楚为什么它给了我表达式必须有一个常量值。
- 我试图找到另一个与 distance(( 相同的函数,可以在 Visual Studio 2012 中使用。
备注:此代码用于从名为"最后一个人"的列下的第一个单元格中查找和获取值。使用代码块时,此代码已经可以了。但我需要使用Visual Studio。
vector<string> data[rowCount];
是可变长度数组的声明。
可变长度数组不是标准的C++功能。一些编译器有自己的语言扩展,允许使用可变长度数组。其他编译器没有这样的语言扩展。
代替数组,您可以使用向量向量,
例如std::vector<std::vector<std::string>> data;
请注意,因为文件是在binaru模式下打开的
ifstream fin(ADDR "reportcard.csv", ios::binary);
那么在一般情况下使用std::getline
函数是不正确的。
while(getline(fin,line)){
rowCount++;
}
当编译器看到以下内容时:
vector<string> data[rowCount];
它从data
开始,向右看。它看到"["并将data
解释为 C 样式数组。然后它看到rowCount
并检查它是否是编译时常量。如果不是,则此代码违反标准。尽管如此,一些编译器仍允许将其作为语言扩展。
总而言之,您已经定义了vector<string>
类型的rowCount
元素的 C 样式数组。您一定将 [] 误认为 (( 或 {}。很可能你想写
std::vector<std::string> data(rowCount); // a vector of rowCount strings
相关文章:
- 将鼠标悬停在问题上时与预期">"相关的代码错误
- VI工作室代码错误无法打开输出文件主.exe
- 引入参数化构造函数后显示 LNK 2019 未解析外部符号的代码错误
- 有关矩阵的代码错误导致分段错误(内核转储)
- C++ Schannel POST 400 错误代码错误请求
- Java 本机访问代码错误:"Invalid memory access"
- 代码错误修改
- C++代码错误分配
- "_FCbuild"不能用作函数 - C/C++ 代码错误
- 我的代码错误类型警告 1 警告 C4018 和更多错误
- 代码错误(从 1 到 100 找到质数)
- OpenCV VisualStudio,C 代码错误尝试访问像素
- 当我使用的教程完全没有错误时,为什么我的代码错误
- 简单的NOOB C 输入/输出问题:代码错误
- 为什么enumProcessModules返回false值和299代码错误
- WinhttpSetoption()失败设置TLSV1.2,带有错误代码错误_internet_incorrect_ha
- 苹果安全传输代码错误
- C++ - 国际象棋主教移动代码错误
- 导入Python库时,嵌入了Python代码错误
- 编译提升C 代码错误