0

For the following lines of code:

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

void telephonebill();
void add();
void edit();
void readFile();
void display();


 string names[100];
 string addresses[100];
 int tn[100];
 int numCalls[100];
 int trunkCalls[100];
 int numMobCalls[100];
 int isdnNum[100];
 int dueD[100];
 int paymentD[100];
 int numC = 0;

I am getting these lines of error:

  1>Project.cpp(12): error C2146: syntax error : missing ';' before identifier 'names'
  1>Project.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1>Project.cpp(12): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1>Project.cpp(13): error C2146: syntax error : missing ';' before identifier 'addresses'
  1>Project.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  1>Project.cpp(13): error C2086: 'int string' : redefinition
  1>Project.cpp(12) : see declaration of 'string'
  1>Project.cpp(13): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int

What is the problem with the declaration of these arrays?

  • 2
    Which line is line 12? And what is *before* line 12? You are including ``? You are doing `using namespace std;` (which many recommend against) or `using std::string;`? – Some programmer dude Dec 26 '14 at 20:56
  • You presumably forgot a `;` before the declaration of `names`. – Columbo Dec 26 '14 at 20:58
  • line 12 is string names[100]; and before it is void display(); –  Dec 26 '14 at 20:58
  • Instead of a bunch of 100-length arrays, you probably want a C++ container filled with a simple structure that contains that data. What you have here is a complete mess. – tadman Dec 26 '14 at 21:05
  • You're not `using` the standard namespace in this source file. As a result, `string` is unknown. `std::string` will work (and is recommended), or (shudder) place `using namespace std;` after your includes. – WhozCraig Dec 26 '14 at 21:07

1 Answers1

2

You are missing the namespace:

You can add the following at the beginning of your file after your includes (note that in a more complex scenario this could be a problem. see here)

using namespace std;

or you can specify the namespace each time you are using string: std::string

Community
  • 1
  • 1
Jérôme
  • 8,016
  • 4
  • 29
  • 35
  • A lot of people recommend against `using namespace` and instead recommend specifying it literally: `std::string`. They're organized into a namespace for a reason. – tadman Dec 26 '14 at 21:06
  • Both solutions are working with this small example. But this is true that in more complex case the using namespace can create issue. – Jérôme Dec 26 '14 at 21:08
  • @tadman significantly more important in a *header* file than a end-point *source* file. It is worth noting, however. – WhozCraig Dec 26 '14 at 21:09