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

时间:2017-08-31 04:16:26

标签: c++ visual-studio compiler-errors visual-studio-2017

据我所知,很多人一直在问这个问题,Stack Overflow上也有类似问题,但我似乎无法理解它们。我希望有人告诉我它为什么会发生,但也发生了什么。

我正在对这个随机程序进行编码,以演示如何将类与主文件分开,然后我开始收到此错误,我正在试着弄清楚如何修复它。

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

代码的相关部分:

的main.cpp

gateway: $("#gateway").val(),

IQclass.h

#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.cpp

#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

错误:

#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;
            };
        };

1 个答案:

答案 0 :(得分:2)

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

自:

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

TO:

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

我想每次都应该包括#include&#34; stdafx.h&#34;第一

相关问题