"error: 'string' does not name a type" when declaring strings

Asked

Viewed 4,507 times

3

Follows the code

class Nome
{
    public:
        Nome(string nome, string sobreNome);
        void exibirNome();
        virtual ~Nome();
    protected:
    private:
         string nome;
         string sobreNome;
         
};

Error

error: expected ')' before 'name'

error: 'string' does not name a type

error: 'string' does not name a type

  • There’s nothing else in your code?

1 answer

4


You have to include the header of string. And the guy’s full name would be std::string, then a using is usually appropriate not to have to describe the namespace every time. In C++ the grouping of names is separated from the grouping of codes (files header).

I rearranged it to avoid redundant code and get more idiomatic.

#include <string>
using namespace std;

class Nome {
        string nome;
        string sobreNome;
    public:
        Nome(string nome, string sobreNome);
        void exibirNome();
        virtual ~Nome(); //espero que vá usar, senão não tem porque criar isto
};

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

  • all that was needed using namespace std;, worked out well worth

Browser other questions tagged

You are not signed in. Login or sign up in order to post.