无效后跟非法?

时间:2012-07-02 17:55:36

标签: c++ class visual-c++ compiler-errors void

我有以下C ++(它还没有做任何事情......)

#include "stdafx.h"
#include <iostream>

using namespace std;

class Ranker 
{
    int up, down;
  public:
    void set_ranks(int, int);
    int rank(int, int, int, double);
}

void Ranker::set_ranks(int a, int b)
{
    up = a;
    down = b;
}

int _tmain(int argc, _TCHAR* argv[])
{
    return 0;
}

当我运行它时,它会在MS V C ++中显示以下错误消息

1>------ Build started: Project: rankclass, Configuration: Debug Win32 ------
1>  rankclass.cpp
1>c:\users\student\desktop\solomon w. c++\rankclass\rankclass\rankclass.cpp(17): error C2628: 'Ranker' followed by 'void' is illegal (did you forget a ';'?)
1>c:\users\student\desktop\solomon w. c++\rankclass\rankclass\rankclass.cpp(18): error C2556: 'Ranker Ranker::set_ranks(int,int)' : overloaded function differs only by return type from 'void Ranker::set_ranks(int,int)'
1>          c:\users\student\desktop\solomon w. c++\rankclass\rankclass\rankclass.cpp(13) : see declaration of 'Ranker::set_ranks'
1>c:\users\student\desktop\solomon w. c++\rankclass\rankclass\rankclass.cpp(18): error C2371: 'Ranker::set_ranks' : redefinition; different basic types
1>          c:\users\student\desktop\solomon w. c++\rankclass\rankclass\rankclass.cpp(13) : see declaration of 'Ranker::set_ranks'
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

为什么这样...... Ranker没有跟随void?!?!

2 个答案:

答案 0 :(得分:19)

Ranker声明后您缺少分号。

应该是:

class Ranker 
{
    int up, down;
  public:
    void set_ranks(int, int);
    int rank(int, int, int, double);
};  // <--- Note semicolon

答案 1 :(得分:4)

类定义后跟分号,在第12行添加一个。