编译器错误C2653:在Visual Studio 17中没有类或名称名称

Compiler error C2653: not a class or namespace name in Visual Studio 17

本文关键字:C2653 错误 Studio Visual 编译器      更新时间:2023-10-16

我知道很多人一直在问这个问题,并且在堆栈溢出上有类似的问题,但我似乎无法理解它们。我希望有人告诉我为什么会发生,但也会发生什么。

我正在编码这个随机程序,以演示如何将类与主文件分开,然后我开始遇到此错误,然后将头发拉出,试图弄清楚如何修复它。

基本上,类.cpp文件不起作用,因为每个功能都在说我的标题文件中的类不是类或命名空间不正确的命名空间。我已经看到了数十次,而且似乎没有任何不合适的拼写,错误或错误地链接,所有文件都在同一项目和文件夹中。

代码的相关部分:

main.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;           
}

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

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...nnn";  //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?nn";               
    cin >> name;                            
}
void IQclass::multiplierSelection(){       (ERROR HERE Line 34)             
    cout << "nWhat should the multiplier be?nn ";                
    cin >> m;                                                       
}
void IQclass::setCycle() {           (ERROR HERE Line 39)
    cout << "nwhat shoud the cycle be?nn";
    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;
            };
        };

错误:

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 | 

当我在笔记本电脑中键入您的代码时(Win7,VS2015)。同样的错误也会发生。我只是在iqclass.cpp文件中修改一些。

来自:

#include "IQclass.h"
#include "stdafx.h"  

to:

#include "stdafx.h"  
#include "IQclass.h"

我认为每次都应首先包括#include" stdafx.h"。