I’ve got:
error a1 was not declared in this scope
Can somebody please explain why this code causes that?
quiz.h
#ifndef QUIZ_H_
#define QUIZ_H_
#include "quiz.cpp" // I deleted this row
// and wrote void quiz(int i);
class A {
private:
int player;
public:
A(int initPlayer); // wrote here = 0
~A();
void foo();
};
#endif /* QUIZ_H_ */
quiz.cpp
#include "quiz.h"
#include <iostream>
using std::cout;
using std::endl;
A::A(int initPlayer = 0){ // deleted = 0
player = initPlayer;
}
A::~A(){
}
void A::foo(){
cout << player;
}
main function
#include "quiz.h"
int main()
{
quiz(7);
return 0;
}
quiz function
#include "quiz.h"
void quiz(int i)
{
A a1(i);
a1.foo();
}
after my modifications I have an error multiple definition of quiz(int)
You should not be including the .cpp file in the header. Remove:
Also, the default value in:
should be in the header file instead.
And edit your question provide the names of all your files, so we can tell you how to compile and link them.