shayan javadi
shayan javadi

Reputation: 41

Error: expected constructor, destructor, or type conversion before ‘(’ token?

When I compile this I get the error title before my name functions in Name.cpp. I've never seen this error before. What's confusing is that there's already a constructor before the three functions in Name.cpp, since that's what the error seems to be talking about.

main.cpp

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

int main()
{

}

Name.h

#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
using namespace std;

class Name
{
    public:
        Name();
        string getFirst(string newFirst);

        string getMiddle(string newMiddle);
        string getLast(string newLast);
        void setFirst();
        void setLast();
        void setMiddle();

    private:
    string First;
    string Middle;
    string Last;
};

#endif // NAME_H

Name.cpp

    #include "Name.h"
    #include <iostream>
    #include <string>
    using namespace std;
    Name::Name()
    {

    }

    Name::setFirst(newFirst){

        Name = newFirst;
        cout << "You entered: " << Name << endl;

    }

    Name::setMiddle(newMiddle){

        Middle = newMiddle;
        cout << "You entered: " << Middle << endl;

    }

     Name::setLast(newLast){

        Last = newLast;
        cout<< "You entered: " << Last << endl;

    }

Upvotes: 0

Views: 3438

Answers (1)

MikeCAT
MikeCAT

Reputation: 75062

You cannot omit type names of arguments. Write ones. Also function prototypes in declaration and definition have to be matched.

Your Name.h should be

#ifndef NAME_H
#define NAME_H
#include <iostream>
#include <string>
using namespace std;

class Name
{
    public:
        Name();
        string getFirst();

        string getMiddle();
        string getLast();
        void setFirst(string newFirst);
        void setLast(string newLast);
        void setMiddle(string newMiddle);

    private:
    string First;
    string Middle;
    string Last;
};

#endif // NAME_H

and Name.cpp should be

#include "Name.h"
#include <iostream>
#include <string>
using namespace std;
Name::Name()
{

}

void Name::setFirst(string newFirst){

    Name = newFirst;
    cout << "You entered: " << Name << endl;

}

void Name::setMiddle(string newMiddle){

    Middle = newMiddle;
    cout << "You entered: " << Middle << endl;

}

void Name::setLast(string newLast){

    Last = newLast;
    cout<< "You entered: " << Last << endl;

}

Upvotes: 3

Related Questions