编译器错误 C2653:不是 Visual Studio 17 中的类或命名空间名称

Ash*_*man 1 c++ compiler-errors visual-studio visual-studio-2017

我知道很多人都在问这个问题,Stack Overflow 上也有类似的问题,但我似乎无法理解它们。我希望有人告诉我为什么会发生这种情况以及发生了什么。

我正在编写这个随机程序来演示如何将类与主文件分开,然后我开始收到此错误,并且我正在绞尽脑汁试图弄清楚如何修复它。

基本上,类 .cpp 文件不起作用,因为每个函数都说我的头文件中的类不是类或命名空间,这是不正确的。我已经看了几十遍了,没有什么地方不对劲,拼写错误或链接错误,所有文件都在同一个项目和文件夹中。

代码的相关部分:

主程序

#include "stdafx.h"         
#include <stdlib.h>         
#include <iostream>         
#include <time.h>           
#include <cstdlib>          
#include "IQclass.h"

using namespace std;                



int main()
{
    IQclass IC;             

    srand(time(NULL));              

    IC.nameInput();                                     
    IC.multiplierSelection();                               
    IC.setCycle();
    IC.forLoop();

    return 0;           
}
Run Code Online (Sandbox Code Playgroud)

IQclass.h

#ifndef IQCLASS_H
#define IQCLASS_H



class IQclass
{
public:

    //IQclass();

    void nameInput();
    void multiplierSelection();
    void setCycle();
    void calc();
    void printIQ();
    void randGen();
    void forLoop();

};

#endif //IQCLASS_H
Run Code Online (Sandbox Code Playgroud)

IQclass.cpp

#include "IQclass.h"
#include "stdafx.h"         
#include <stdlib.h>         
#include <iostream>     
#include <time.h>           
#include <cstdlib>          

int y;                  
long int a;             
int m;                  
 int c;
char name[40];

using namespace std;

/* IQclass::IQclass()
{
    cout << "\ninitializing...\n\n\n";  //dont ever do this i did it to be funny 
typicaly constructers are used to set variables to a defualt state when the 
object is created
    _sleep(3000);
}

*/

void IQclass::nameInput()         (ERROR HERE Line 28)
 {
    cout << "What is your name?\n\n";               
    cin >> name;                            
}

void IQclass::multiplierSelection(){       (ERROR HERE Line 34)             
    cout << "\nWhat should the multiplier be?\n\n ";                
    cin >> m;                                                       
}

void IQclass::setCycle() {           (ERROR HERE Line 39)
    cout << "\nwhat shoud the cycle be?\n\n";
    cin >> c;
}

void IQclass::calc() {             (ERROR HERE Line 44)
    a = y*m;                                        
 }

void IQclass::printIQ() {           (ERROR HERE Line 48)
    cout << name << "'s IQ is probably: " << y << endl << "That times: " << m << 
" is " << a << endl << endl;
}

void IQclass::randGen() {                    (ERROR HERE Line 52)
    y = rand() % 160;           
};

   void IQclass::forLoop() {            (ERROR HERE Line 56)
        for (int x = 0; x < c;) {               
            randGen();                          
            calc();                     
            printIQ();                      
            x = x + 1;
            };
        };
Run Code Online (Sandbox Code Playgroud)

错误:

C2653 'IQclass': is not a class or namespace name |File IQclass.cpp | line 28 | 
C2653 'IQclass': is not a class or namespace name |File IQclass.cpp | line 34 | 
C2653 'IQclass': is not a class or namespace name |File IQclass.cpp | line 39 | 
C2653 'IQclass': is not a class or namespace name |File IQclass.cpp | line 44 | 
C2653 'IQclass': is not a class or namespace name |File IQclass.cpp | line 48 | 
C2653 'IQclass': is not a class or namespace name |File IQclass.cpp | line 52 | 
C2653 'IQclass': is not a class or namespace name |File IQclass.cpp | line 56 | 
Run Code Online (Sandbox Code Playgroud)

Pen*_*nny 8

当我在我的笔记本电脑(win7,VS2015)中输入你的代码时。也会发生同样的错误。我只是在 IQClass.cpp 文件中修改了一点。

从:

#include "IQclass.h"
#include "stdafx.h"  
Run Code Online (Sandbox Code Playgroud)

到:

#include "stdafx.h"  
#include "IQclass.h"
Run Code Online (Sandbox Code Playgroud)

我认为每次我们都应该首先包含#include“stdafx.h”。