Question

[Solved] error: expected unqualified-id before ‘int’

I’m getting the following error when I try to compile my code. I’ve read through other questions from people who get the same error but none of the answers are relevant to me.

user.cpp:15:7: error: expected unqualified-id before ‘int’
  User(int user_id, string user_name, int user_year, int user_zip)
       ^
user.cpp:15:7: error: expected ‘)’ before ‘int

Any help would be appreciated.

user.cpp:

#include "user.h"

using namespace std;

User(int user_id, string user_name, int user_year, int user_zip)
{
   id = user_id;
   name = user_name;
   year = user_year;
   zip = user_zip;
   friends = {};
}

~User()
{

}

void User::add_friend(int id)
{
   friends.push_back(id);
}

void User::delete_friend(int id)
{
   for (int i = 0; i < friends.size();++i)
   {
      if (friends[i] == id)
      {
         friends.erase(vec.begin() + i);
      }
   }
}

int User::getID()
{
   return id;
}

string User::getName()
{
   return name;
}

int User::getYear()
{
   return year;
}

int User::getZip()
{
   return zip;
}

vector<int>* User::getFriends()
{
   return &friends;
}

user.h:

#ifndef USER_H
#define USER_H
#include <string>
#include <vector>

class User {

 public:

    User(int user_id, std::string user_name, int user_year, int user_zip);
    ~User();
    void add_friend(int id);
    void delete_friend(int id);
    int getID();
    std::string getName();
    int getYear();
    int getZip();
    std::vector<int>* getFriends();

 private:

    int id;
    std::string name;
    int year;
    int zip;
    std::vector<int> friends;

};

#endif
Enquirer: Ben

||

Solution #1:

This line:

User(int user_id, string user_name, int user_year, int user_zip)

is not a definition of a constructor of User but rather an instantiation of User object, and therefore it expects a list of arguments that are passed to the constructor, and int is not a valid identifier that can be passed.

The correct way to define a constructor is:

User::User(int user_id, string user_name, int user_year, int user_zip)

Similarly, for the destructor:

User::~User()
Respondent: Ishamael

Solution #2:

Try the following line:

User::User(int user_id, std::string user_name, int user_year, int user_zip)
Respondent: Marvin Wang

Solution #3:

Should be

User::User(int user_id, std::string user_name, int user_year, int user_zip)

and

User::~User()
Respondent: James Picone

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy