错误:"int"之前的预期unqualified-id [英] error: expected unqualified-id before ‘int’

查看:95
本文介绍了错误:"int"之前的预期unqualified-id的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我尝试编译代码时,出现以下错误.我已经阅读了同样错误的人的其他问题,但没有一个答案与我相关.

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’

任何帮助将不胜感激.

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

推荐答案

此行:

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

不是对 User 的构造函数的定义,而是对 User 对象的实例化,因此它期望传递给该构造函数的参数列表,以及 int 不是可以传递的有效标识符.

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

这篇关于错误:"int"之前的预期unqualified-id的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆