High Low program syntax errors
main.cpp
#include <iostream>
#include <cmath>
#include "Highlow.h"
using namespace std;
int main()
{
Highlow generate;
generate.UserInput();
system("pause");
}
------------
Highlow.cpp
#include <iostream>
#include <cmath>
#include "Highlow.h"
using namespace std;
void Highlow::UserInput()
{
int RandomValue;
int Player1;
RandomValue = rand();
cin >> Player1;
do {cout << "Try Again" << endl;}
while
(RandomValue != Player1);
cout << "You've Guess Correctly!" << endl;
}
-----------
Highlow.h
#include <iostream>
#include <cmath>
using namespace std;
class Highlow
{
private:
public:
Highlow::Highlow();
void Highlow::UserInput();
};
Errors
--------
Error 1 error LNK2019: unresolved external symbol "public: __thiscall Highlow::Highlow(void)" (??0Highlow@@QAE@XZ) referenced in function _main main.obj
Error 2 fatal error LNK1120: 1 unresolved externals C:\Visual Studio 2005\Projects\Highlow\Debug\HighlowGame.exe 1
# 1 Re: High Low program syntax errors
1) Remove the unnecessary header files from HighLow.h. Nowhere in the header are you calling any stream or math functions.
2) Do not put using namespace std in a header file.
3) Your header file has compiler errors. The "Highlow::Highlow", etc. in the header file is incorrect.
4) Header files should have the proper include guards so that multiple inclusions do not incur an error.
5) Use code tags when posting code.
Here is the proper way that the header file should have been written.
#ifndef HIGHLOW_H
#define HIGHLOW_H
class Highlow
{
private:
public:
Highlow();
void UserInput();
};
#endif
6) Your linker error is that you did not implement the constructor function you declared. You declared it, but where is the actual function that will be performed?
class Highlow
{
private:
public:
Highlow(); // Where is this function?
//...
};
//...
Highlow generate; // You're calling the function here.
Where is the Highlow constructor function implemented? If you can't implement it, then remove the definition from the header. Then the compiler will use the compiler-generated default constructor.
Regards,
Paul McKenzie
# 2 Re: High Low program syntax errors
Sorry about the messy post, now they are all in code format. I have changed the header format to your specifications, but the problem persists. Please be patient with me, I am new to c++ programming.
#ifndef HIGHLOW_H
#define HIGHLOW_H
class Highlow
{
private:
public:
Highlow::Highlow();
void Highlow::UserInput();
};
#endif
#include <iostream>
#include <cmath>
#include "Highlow.h"
using namespace std;
void Highlow::UserInput()
{
int RandomValue;
int Player1;
RandomValue = rand();
cin >> Player1;
do {cout << "Try Again" << endl;}
while
(RandomValue != Player1);
cout << "You've Guess Correctly!" << endl;
}
#include <iostream>
#include "Highlow.h"
#include <cmath>
using namespace std;
int main()
{
Highlow generate;
generate.UserInput();
system("pause");
}
# 3 Re: High Low program syntax errors
3) Your header file has compiler errors. The "Highlow::Highlow", etc. in the header file is incorrect.
6) Your linker error is that you did not implement the constructor function you declared. You declared it, but where is the actual function that will be performed?
You still have these problems in your code
# 4 Re: High Low program syntax errors
Sorry about the messy post, now they are all in code format. I have changed the header format to your specifications, but the problem persists. Please be patient with me, I am new to c++ programming.
The header is still not correct and you didn't follow step 3 of what I outlined. Here is your code compiled with an ANSI C++ compiler. Note the errors:
#ifndef HIGHLOW_H
#define HIGHLOW_H
class Highlow
{
private:
public:
Highlow::Highlow();
void Highlow::UserInput();
};
#endif
Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
Copyright 1988-2007 Comeau Computing. All rights reserved.
MODE:strict errors C++ C++0x_extensions
"ComeauTest.c", line 9: error: qualified name is not allowed in member declaration
Highlow::Highlow();
^
"ComeauTest.c", line 10: error: qualified name is not allowed in member declaration
void Highlow::UserInput();
^
2 errors detected in the compilation of "ComeauTest.c".
In strict mode, with -tused, Compile failed
To fix this, please get rid of the "Highlow::" from those definitions within the class, as I outlined in my first post. It doesn't matter if your compiler allows it, it is still incorrect and non-standard.
Second, you still haven't implemented the constructor function for Highlow.
You declared a function in your class called "Highlow()". Since this is a constructor function, it will be called automatically when you construct a HighLow object.
But where is the code for this HighLow() function? You haven't written it, and that is what the linker is complaining about.
To see if you understand, what happens on this line of code? What functions are called?
Highlow generate;
Yes, the constructor is called. So where is it? Just declaring it isn't enough. You need to implement it (i.e. write the code for it).
Regards,
Paul McKenzie