0

Here is my file.h :

 #define MAXCOMPONENTS 20
 #include <string>
 #include <string.h>
 #include <iostream>
 class file{
public:
    file(char const * filename);
    virtual ~file();

    void Takeinfocomponents();
    void Takeshape();

    void Getvalue(int i);
    char *Getcomponents();
    char *Getcolor();


protected:
private:
    char const * filename;
    String shape;
    int value[MAXCOMPONENTS];
    char components[MAXCOMPONENTS];
    char color[MAXCOMPONENTS];

};

And my file.cpp :

 #include <fstream>
 #include <iostream>
 #include <string.h>
 #include <string>
 #include "file.h"
 using namespace std;

 file::file(char const* filename)
 {
     cout << "constructor/fichier:" << filename << endl;
     ifstream fichier(filename,ios::in);
     if(fichier){
         this->filename=filename;
         fichier.close();
         Takeshape();
         Takeinfocomponents();
     }else{
         cout << "File name invalid." << endl;
     }
 }

 file::~file()
 {

 }

 char* file::Getcolor(){
     return this->color;
 }

 char* file::Getcomponents(){
    return this->components;
 }

 void file::Getvalue(int i){
    cout << this->value[i] << endl;
 }


 void file::Takeinfocomponents(){ // pic up name of components, his number and his color
    cout << "Takeinfocomponents/fichier:" << filename << endl;
    ifstream fichier(this->filename,ios::in);
    ifstream stop(this->filename,ios::in);
    string line;
    int i=0;
    getline(fichier,line);
    getline(stop,line);
    getline(stop,line);
    while(line!="/" && i!=99){ // take all informations while the stop signal isn't read
        getline(stop,line);
        fichier >> this->components[i] >> this->value[i] >> this->color[i];
        cout << this->components[i] << this->value[i] << this->color[i] << endl;
        i++;
   }
   fichier.close();
}

void file::Takeshape(){ // pic up the shape in .txt
   cout << "Takeshape" << endl;
   fstream fichier(this->filename,ios::in);
   string shape;
   fichier >> shape;
   this->shape=shape;
   fichier.close();
}

This is a part of a larger programm who make graphic from informations ( from the .txt ), this part is use to pic up informations from the .txt.

The problem come from the declaration of the :

 String shape;

He told me that string is not a name type. I've tried with a small "s" :

 string shape;

But this ain't working. I've the impression that i miss a very small things that could unlock my problem. Thx for help.

Notabene : I'm french and my english is not this good, please answer like i was a little child ahah !

  • 5
    You probably want `std::string`. – Mark Ransom Oct 20 '15 at 16:44
  • Just to follow up on @MarkRansom's comment: this is about C++ namespaces. This will give you a good overview: http://www.cprogramming.com/tutorial/namespaces.html – Oliver Dain Oct 20 '15 at 16:47
  • Whoa thx a lot ! But i do not anderstand why we have to put "std::" – Guillaume COURMONT Oct 20 '15 at 16:50
  • In addition to the string/String problem, storing a pointer to the filename is a really bad idea. The location it is pointing to is likely to change or go away without notice. Storing the name in another `std::string` is much safer. – Bo Persson Oct 20 '15 at 17:31

3 Answers3

1

You have to explicitly state the namespace:

std::string shape;

You shouldn't pollute the namespace in the headers, so using namespace std is not an option here.

See also the question about namespace pollution. If you just need strings, prefer to use

using std::string;

in the cpp file.

Community
  • 1
  • 1
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
0

C++ uses the concept of a namespace. A namespace is used to group types, variables, etc. together in a meaningful way, regardless of the number of header files those types or variables are spread across.

In this example, the string type is inside the std namespace. std is short for Standard Template Library, and it is the namespace that most of C++'s library classes, etc. are stored in.

The correct way of accessing type inside a namespace is namespace::type, so the correct way of accessing the string type inside the std namespace is std::string. You can also write using namespace std to access the types in std without having to write std:: each time, but doing this in a global scope is a bad idea, because it pollutes the global namespace.

In the code you posted, string shape; appears before using namespace std, as the #include "file.h" appears before it. Therefore, it won't take effect.

Sonic Atom
  • 436
  • 5
  • 15
0

To be able to use the string class and create string objects, you need to include...

#include <string>

... at the top of your header files.

You do not need...

#include <string.h>

The string class, like all STL classes, is part of the std namespace. If you do not want to write std:: before every class name, you can simply state...

using namespace std;

... at the top of your header files so that instead of...

std::string shape;

... you can simply use...

string shape;
dspfnder
  • 1,135
  • 1
  • 8
  • 13