Other classes we've made in class have been done that way, and have worked. Do you want me to upload another Visual Studio solution for you to check?
Oooooh, Visual Studio! That explains everything.
I can't guarantee this is the issue, but I can tell you my personal experience. Half the times I have tried to code anything in Visual Studio I have gotten all sorts of weird unexplainable errors. Even after triple checking my code I have been unable to find anything wrong. The problem has always been some mysterious thing hidden somewhere in the Visual Studio that was not correct. For example, one time I had created the wrong kind of project, so even a straightforward hello world made all kinds of errors when I pressed the run button.
This is why I do everything with vim and terminal on *NIX.
Clearly I either haven't learned enough C++, or we are somehow talking about different things? I have other classes that have an int main() in another class, and they run.
Other classes we've made in class have been done that way, and have worked. Do you want me to upload another Visual Studio solution for you to check?
Oooooh, Visual Studio! That explains everything.
I can't guarantee this is the issue, but I can tell you my personal experience. Half the times I have tried to code anything in Visual Studio I have gotten all sorts of weird unexplainable errors. Even after triple checking my code I have been unable to find anything wrong. The problem has always been some mysterious thing hidden somewhere in the Visual Studio that was not correct. For example, one time I had created the wrong kind of project, so even a straightforward hello world made all kinds of errors when I pressed the run button.
This is why I do everything with vim and terminal on *NIX.
We have to use Visual Studio. It's a requirement of this class. Visual Studio has always worked for me before.
In C/C++, the main function is global and not part of any class. This is the entry point for your program. You defined a second main function in your TestHW2 class which is a member function of that class. However, it doesn't do anything. If you wanted to call that function, it would be something like TestHW2* test = new TestHW2(); test->main();
And before you say anything, said teacher is not a game developer. Just a programmer who was hired to be a code teacher for our department. He's coded all the hell over.
Well...I'm just doing what we've been told to do. I could get rid of TestHW2, but the assignment requires that I use it.
1>TestHW2.obj : error LNK2019: unresolved external symbol "public: int __thiscall Deck::cardValue(int)" (?cardValue@Deck@@QAEHH@Z) referenced in function _main 1>TestHW2.obj : error LNK2019: unresolved external symbol "public: void __thiscall Deck::cardAsString(int)" (?cardAsString@Deck@@QAEXH@Z) referenced in function _main 1>TestHW2.obj : error LNK2019: unresolved external symbol "public: int __thiscall Deck::deal(void)" (?deal@Deck@@QAEHXZ) referenced in function _main 1>TestHW2.obj : error LNK2019: unresolved external symbol "public: int __thiscall Deck::cardCount(char)" (?cardCount@Deck@@QAEHD@Z) referenced in function _main 1>c:\users\axel\documents\visual studio 2010\Projects\AlexanderDunnHomework2\Debug\AlexanderDunnHomework2.exe : fatal error LNK1120: 4 unresolved externals
Looked at the code. Andrew is right. Also, wow is the if statement the only thing you have learned so far? That is the most unfolded code I have seen in a loooong time. Let me give you just one hint, and you apply similar ideas across the board. There's no reason for that program to be longer than my screen.
With a lookup table! int cardValue(int cardNum) { static const int cards[44] = { 11,11,11,11, 2,2,2,2, 3,3,3,3, 4,4,4,4, 5,5,5,5, 6,6,6,6, 7,7,7,7, 8,8,8,8, 9,9,9,9, 10,10,10,10 } return cards[cardNum]; }
Or even better, use math to calculate the result instead of having it hard-coded. int cardValue(int cardNum) { // take the remainder when dividing by four and subtract iit // that gets you the next lowest multiple of four int newCardNum = cardNum - (cardNum % 4); if( newCardNum == 0){ // the first group breaks the pattern and returns 11 return 11; }else{ // every other group follows the pattern // take the value, divide by four and add 1 // the 12 group returns 4, the 16 group returns 5, etc. return (newCardNum / 4) + 1; } }
It's in Testhw2.h and Testhw2.cpp. Nada. Still have the same errors. I'm aware it should only need to be in Testhw2.h, since Testhw.cpp includes it, but I tested anyways.
@Apreche - I have been taught more things in other languages, but we haven't been taught any of the more cool C++ stuff. It's only week 3 of the class.
So this is probably relevant to people in this thread. Cocode.io is Google Docs for programmers. GitHub Gist in real time, paired-programming without sharing a keyboard, however you want to put it. It's pretty awesome, but they're in the early phases, so it's invite-only (it took < 1 day to receive my invitation) and they only have syntax highlighting for 9 programming/markup languages (Java, JavaScript, Python, HTML, C/C++, and Ruby are all there, so that covers a pretty large portion of what people need). http://cocode.io/beta/
So this is probably relevant to people in this thread. Cocode.io is Google Docs for programmers. GitHub Gist in real time, paired-programming without sharing a keyboard, however you want to put it. It's pretty awesome, but they're in the early phases, so it's invite-only (it took < 1 day to receive my invitation) and they only have syntax highlighting for 9 programming/markup languages (Java, JavaScript, Python, HTML, C/C++, and Ruby are all there, so that covers the a pretty large portion of what people need). http://cocode.io/beta/
Deck.h and Deck.cpp seem to be functioning properly. The header for those works, Deck.cpp references stuff in Deck.h. I dunno. The compiler seems to be working...
It's in Testhw2.h and Testhw2.cpp. Nada. Still have the same errors. I'm aware it should only need to be in Testhw2.h, since Testhw.cpp includes it, but I tested anyways.
@Apreche - I have been taught more things in other languages, but we haven't been taught any of the more cool C++ stuff. It's only week 3 of the class.
I didn't use any special C or C++ stuff. I only used techniques that are available in almost every programming language, including BASIC.
It's in Testhw2.h and Testhw2.cpp. Nada. Still have the same errors. I'm aware it should only need to be in Testhw2.h, since Testhw.cpp includes it, but I tested anyways.
@Apreche - I have been taught more things in other languages, but we haven't been taught any of the more cool C++ stuff. It's only week 3 of the class.
I didn't use any special C or C++ stuff. I only used techniques that are available in almost every programming language, including BASIC.
*Shrug* I won't say that I wrote it the most efficiently. You have more coding experience for me, so I tend to just do what is the simplest thing I know. I should probably know lookup tables, and I will try and use those efficiently in the future. But there's no reason to rewrite all my code, because this is an intro course, and the professor doesn't particularly care.
Axel, you really need to get a strong grasp on C++ classes. Please read through this document and try to understand some of my earlier comments and why this whole TestHW2 class is not needed.
I understand what you're saying, but the fact is, for this coding project, I needed to have those classes. It was written in the requirements. This teacher grades off a rubric, and I would've lost points without it. I get that I can just have the main function outside of a class, that the class is pretty much useless, but he wants me to make a class for it, and the program runs just fine now, so...Yeah.
Actually, Andrew's point is that your main method isn't actually inside your TestHW2 class to begin with. If it was actually inside, it would be TestHW2::main, not just main, and then you would need an actual main() method which would simply call TestHW2::main() - i.e. your code would have two methods, something like: int TestHW2::main() { // Do all the work here. } int main() { TestHW2* test = new TestHW2(); return test->main(); }
Having a main inside a class is only necessary in something like Java where you can't have a method outside of a class - I think Pegu is right on the money and your teacher is mostly used to Java. It's still possible you would want to use a class to wrap a function in C++, but typically this would only be for polymorphism.
*Shrug* I won't say that I wrote it the most efficiently. You have more coding experience for me, so I tend to just do what is the simplest thing I know. I should probably know lookup tables, and I will try and use those efficiently in the future. But there's no reason to rewrite all my code, because this is an intro course, and the professor doesn't particularly care.
You say this now, but if there's anything wrong with your code it's just going to take longer to debug as well and you'll end up spending more time than you would rewriting it.
Also, regardless of the quality of the course, you'll learn more if you try to do a good job.
Comments
I can't guarantee this is the issue, but I can tell you my personal experience. Half the times I have tried to code anything in Visual Studio I have gotten all sorts of weird unexplainable errors. Even after triple checking my code I have been unable to find anything wrong. The problem has always been some mysterious thing hidden somewhere in the Visual Studio that was not correct. For example, one time I had created the wrong kind of project, so even a straightforward hello world made all kinds of errors when I pressed the run button.
This is why I do everything with vim and terminal on *NIX.
TestHW2* test = new TestHW2();
test->main();
#pragma once
#include <iostream>
class Test
{
public:
Test(void);
~Test(void);
int main();
};
#include "Test.h"
using namespace std;
Test::Test(void)
{
}
Test::~Test(void)
{
}
int main()
{
// read in three numbers
double grade1, grade2, grade3;
cout << "Enter three grades, separated by spaces: ";
cin >> grade1 >> grade2 >> grade3;
// average the data
double avg = (grade1 + grade2 + grade3) / 3;
cout << endl << endl; // skip a couple of lines
cout << "You entered: " << grade1 << ", " << grade2 << ", "
<< grade3 << endl;
cout << "Average is: " << avg << endl;
// Make command screen stop until a character is entered.
char chr;
cout << endl << "Enter a char to continue: ";
cin >> chr;
}
That runs.
By someone's who's used C++ for 12 years.
You should read this
Well...I'm just doing what we've been told to do. I could get rid of TestHW2, but the assignment requires that I use it.
1>TestHW2.obj : error LNK2019: unresolved external symbol "public: int __thiscall Deck::cardValue(int)" (?cardValue@Deck@@QAEHH@Z) referenced in function _main
1>TestHW2.obj : error LNK2019: unresolved external symbol "public: void __thiscall Deck::cardAsString(int)" (?cardAsString@Deck@@QAEXH@Z) referenced in function _main
1>TestHW2.obj : error LNK2019: unresolved external symbol "public: int __thiscall Deck::deal(void)" (?deal@Deck@@QAEHXZ) referenced in function _main
1>TestHW2.obj : error LNK2019: unresolved external symbol "public: int __thiscall Deck::cardCount(char)" (?cardCount@Deck@@QAEHD@Z) referenced in function _main
1>c:\users\axel\documents\visual studio 2010\Projects\AlexanderDunnHomework2\Debug\AlexanderDunnHomework2.exe : fatal error LNK1120: 4 unresolved externals
Replace this kind of gigantic thing
...
if(cardNum == 0 || cardNum == 1 || cardNum == 2 || cardNum == 3)
{
return 11;
}
...
With a lookup table!
int cardValue(int cardNum)
{
static const int cards[44] = {
11,11,11,11,
2,2,2,2,
3,3,3,3,
4,4,4,4,
5,5,5,5,
6,6,6,6,
7,7,7,7,
8,8,8,8,
9,9,9,9,
10,10,10,10
}
return cards[cardNum];
}
Or even better, use math to calculate the result instead of having it hard-coded.
int cardValue(int cardNum)
{
// take the remainder when dividing by four and subtract iit
// that gets you the next lowest multiple of four
int newCardNum = cardNum - (cardNum % 4);
if( newCardNum == 0){
// the first group breaks the pattern and returns 11
return 11;
}else{
// every other group follows the pattern
// take the value, divide by four and add 1
// the 12 group returns 4, the 16 group returns 5, etc.
return (newCardNum / 4) + 1;
}
}
@Apreche - I have been taught more things in other languages, but we haven't been taught any of the more cool C++ stuff. It's only week 3 of the class.
http://cocode.io/beta/
Axel, if you want I'll set aside some time to Skype/pair program with you next week. This guy obviously isn't teaching you properly.
You have
int deal()
when it should be
int Deck::deal()
Do the same for the rest of Deck's functions.
Thank you.
Now to fix my logic errors. Your help has been appreciated.
int TestHW2::main() {
// Do all the work here.
}
int main() {
TestHW2* test = new TestHW2();
return test->main();
}
Having a main inside a class is only necessary in something like Java where you can't have a method outside of a class - I think Pegu is right on the money and your teacher is mostly used to Java. It's still possible you would want to use a class to wrap a function in C++, but typically this would only be for polymorphism.
Also, regardless of the quality of the course, you'll learn more if you try to do a good job.