Reputation: 117
I'm using Xcode for C++ on my computer while using Visual Studio at school. The following code worked just fine in Visual Studio, but I'm having this problem when using Xcode.
clock c1(2, 3, 30);
Everything works just fine, but it keeps giving me this error that says "Expected ';' before 'c1'"
Fine, I put the ';' .. but then, it gives me this error: "'c1' was not declared in this scope"
Here's the whole header code:
#include <iostream>
using namespace std;
class clock
{
private:
int h;
int m;
int s;
public:
clock(int hr, int mn, int sec);
};
clock::clock(int hr, int mn, int sec)
{
h = hr;
m = mn;
s = sec;
}
Here's the whole .cpp code:
#include "clock.h"
int main()
{
clock c1(2, 3, 30);
return 0;
}
I stripped everything down to where I had the problem. Everything else, as far as I know, is irrelevant since the problem remains the same with just the mentioned above.
Thanks in advance!
Upvotes: 0
Views: 6057
Reputation: 39404
You should put your clock
class into a namespace to stop it conflicting with clock()
.
Well done for stripping everything else out before asking the question.
Since you have stripped lots of things out, you will be putting it all back again. There may be other problems that you encounter:
using namespace std;
in a header.Upvotes: 0
Reputation: 54290
Your clock class definition is clashing with clock_t clock()
from <ctime>
(see here).
You can get around it by specifying that you want the class and not the function in the way that Johannes described, but really you should just rename your class so that it doesn't clash with a standard function. That's the most practical solution.
Just to reiterate what Johannes said, do not put using namespace std;
in a header. That causes the std
namespace to be injected into every file that includes your header, which is bound to cause identifier collisions at some point. If you really need it, but it in the source file only, as nothing includes that.
Upvotes: 4
Reputation: 507105
There is a function clock
that will hide your clock
class of the same name. You can work this around by saying
class clock c1(2, 3, 30);
It's very bad practice to do using namespace std;
in a header. Instead put that line into the cpp
file only. It may solve your problem if you remove that line (if the name comes from namespace std::
instead of from the global namespace originally).
Upvotes: 6