Reputation: 325
I understand that a lot of people have been asking this question and there are similar ones on Stack Overflow, but I can't seem to understand them. I am hoping that someone tells me why it's happening but also what is happening.
I was coding this random program to demonstrate how to separate classes from the main file and I started getting this error and I am pulling my hair out trying to figure out how to fix it.
Basically, the class .cpp file is not working because every function is saying that the class in my header file is not a class or a namespace which is incorrect. I have looked it over dozens of times and nothing seems out of place, spelled incorrectly or linked incorrectly, all the files are in the same project and folder.
Relevant parts of the code:
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...\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;
};
};
the error:
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 |
Upvotes: 1
Views: 5877
Reputation: 656
when I key in your code in my laptop(win7, VS2015). The same error happens. I just modify a little in IQClass.cpp file.
From:
#include "IQclass.h"
#include "stdafx.h"
TO:
#include "stdafx.h"
#include "IQclass.h"
I think everytime, we should include #include "stdafx.h" first.
Upvotes: 9