Reputation: 59
I am fairly new to cpp but have been in c# for a while. I am trying to run a simple console application but I receive this LNK2001 error message.
I have the main.cpp, and have added another class, Zeus, with files, Zeus.h and Zeus.cpp.
Here is the main.cpp:
#include "Zeus.h"
#include <iostream>
int main()
{
Zeus::tick = 25.0;
using std::cout;
cout << "nothing";
}
Here is the Zeus.h:
static class Zeus
{
public:
static void testing(void);
public:
static double tick;
};
And here is the Zeus.cpp:
void Zeus::testing(void)
{
//Doesnt get this far
//But eventually something like
// cout << "test " << Zeus::tick;
}
And here is the error message:
Error 20 error LNK2001: unresolved external symbol "public: static double Zeus::tick"
Thanks,
Upvotes: 0
Views: 325
Reputation: 103
In the main()
function you have, what do you mean by a statement Zeus::tick = 25.0;
?
Zeus
is a class. So to access the individual elements of it you need to create its instance. Its just like a structure where you first create its instance to access its individual elements.
Try the following:
int main() {
Zeus myobject;
myobject.tick = 25.0;
/* Rest of the definition */
}
Upvotes: 0
Reputation: 227548
You need to define Zeus::tick
, typically you would to that in the in the Zeus.cpp
file. You have only declared it.
double Zeus::tick = 0.0;
Also, there is no static class
in C++.
As an aside, free functions can be put in namespaces, as opposed to being static functions of classes. This is the preferred way in C++, unless there are strong reasons for the function to be static.
namespace Dionysus {
void testing();
}
Upvotes: 1
Reputation: 76458
As the error message says: there's no definition of Zeus::tick
. Add this to Zeus.cpp:
double Zeus::tick;
Oh, and in Zeus.h remove the static
from
static class Zeus
Upvotes: 0