简体   繁体   中英

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;
}
  1. im trying to figure out why it gave me the expression must have a constant value.
  2. im trying to find another function that works the same as distance() that can be used in visual studio 2012.

note: this code is for finding and getting the value from the first cell under a column called "LAST PERSON". This code is already okay when using codeblocks. but i need to use visual studio.

This

vector<string> data[rowCount];

is a declaration of a variable length array.

Variable length arrays is not a standard C++ feature. Some compilers have their own language extensions that allow to use variable length arrays. Other compilers do not have such a language extension.

Instead of the array you could use a vector of vectors as for example

std::vector<std::vector<std::string>> data;

Pay attention to that as the file is opened in the binaru mode

ifstream fin(ADDR "reportcard.csv", ios::binary);

then using the function std::getline is not correct in general case.

while(getline(fin,line)){
    rowCount++;
}

When the compiler sees this:

vector<string> data[rowCount];

it starts from data and looks to the right. It sees '[' and interprets data to be a C-style array. Then it sees rowCount and checks if it is a compile-time constant. If not, this code is against the standard. Still, some compilers allow this as a language extension.

All in all, you've defined a C-style array of rowCount elements of type vector<string> . You must have mistaken [] for () or {}. Most likely you wanted to write

std::vector<std::string> data(rowCount); // a vector of rowCount strings 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM