Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
267 views
in Technique[技术] by (71.8m points)

c++ - 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...