Classes-- stuck in the mire of syntax

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Classes-- stuck in the mire of syntax

 
0
  #1
Oct 31st, 2006
Hi,

I am working on a fairly simple project (it will grow more in time as planned) but I am stuck in a spot which I cannot solve yet.

The operation of this prog at this point is:
  • Start. Ask user for input choice (only 1 choice available at this point)
  • Cin will take user to another screen (another file, Look.cpp) This file will run and request more input.
  • This input will run
  • End.
My problem is how to "link" Main.cpp and the cin input to Look.cpp where the action is located, that is, more input is requested. This will be done using classes.

I consider the action of going to Look.cpp a "function" as declared in Look.h. Maybe this action is not a true function; it does no mathematical operation-- and does it return anything? No, it is void.

I feel very close to solving this but errors keep getting thrown. Perhaps it is just my syntax in the use of objects, referencing, etc?

Any help is greatly appreciated-- thank you in advance.

Arnold L.


Look.h
  1. class Look {
  2. public:
  3. float shipPos ;
  4. float satPos;
  5. float interior;
  6.  
  7. void printShipPos(); // new function declaration
  8.  
  9. Look();
  10. ~Look();
  11. };

Look.cpp
  1. #include <iostream>
  2. #include "Look.h"
  3. using namespace std;
  4.  
  5.  
  6. void Look::printShipPos() //.h function set here, refers to class
  7. // Look via "::"
  8. {
  9.  
  10. cin >> shipPos; // this shipPos is the Look class member variable
  11.  
  12. if (shipPos <= 3.80) {
  13. std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
  14. }
  15. else if (shipPos <= 7.60) {
  16. std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
  17. }
  18. else if (shipPos <= 11.40) {
  19. std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl;
  20. }
  21. else if ((shipPos >= 15.20) && (shipPos < 19.00)) {
  22. std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl;
  23. }
  24. else if ((shipPos >= 19.00) && (shipPos < 22.80)){
  25. std::cout << "La Paz, Baja, Mexico [In Transmission Range]"<< endl;
  26. }
  27. else if ((shipPos >= 22.80) && (shipPos < 26.60)){
  28. std::cout << "Gulf of Mexico [In Transmission Range]"<< endl;
  29. }
  30. else if ((shipPos >= 26.60) && (shipPos < 30.40)){
  31. std::cout << "Tampa, Florida [In Transmission Range]"<< endl;
  32. }
  33. else if ((shipPos >= 30.40) && (shipPos < 34.20)){
  34. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 4320 miles from Lisbon, Portugal"<< endl;
  35. }
  36. else if ((shipPos >= 34.20) && (shipPos < 38.00)){
  37. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 3240 miles from Lisbon, Portugal"<< endl;
  38. }
  39. else if (shipPos <= 38.00) {
  40. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 2160 miles from Lisbon, Portugal"<< endl;
  41. }
  42. else if (shipPos <= 41.80) {
  43. std::cout << "Canary Islands [Out of Transmission Range]"<< endl;
  44. }
  45. else if (shipPos <= 45.60) {
  46. std::cout << "Lisbon, Portugal [Out of Transmission Range]"<< endl;
  47. }
  48. else if (shipPos <= 49.40) {
  49. std::cout << "Tripoli, Libya [Out of Transmission Range]"<< endl;
  50. }
  51. else if (shipPos <= 53.20) {
  52. std::cout << "Baghdad, Iraq [Out of Transmission Range]"<< endl;
  53. }
  54. else if (shipPos <= 57.00) {
  55. std::cout << "Eastern Iran [Out of Transmission Range]"<< endl;
  56. }
  57. else if (shipPos <= 60.80) {
  58. std::cout << "Central Tajikistan [Out of Transmission Range]"<< endl;
  59. }
  60. else if (shipPos <= 64.60) {
  61. std::cout << "Kathmandu, Nepal [Out of Transmission Range]"<< endl;
  62. }
  63. else if (shipPos <= 68.40) {
  64. std::cout << "Hanoi, Vietnam [Out of Transmission Range]"<< endl;
  65. }
  66. else if (shipPos <= 72.20) {
  67. std::cout << "Hong Kong, China [Out of Transmission Range]"<< endl;
  68. }
  69. else if (shipPos <= 76.00) {
  70. std::cout << "Pacific (open waters) [Out of Transmission Range] 5400 miles from French Polynesia"<< endl;
  71. }
  72. else if (shipPos <= 79.80) {
  73. std::cout << "Pacific (open waters) [Out of Transmission Range] 4320 miles from French Polynesia"<< endl;
  74. }
  75. else if (shipPos <= 83.60) {
  76. std::cout << "Pacific (open waters) [Out of Transmission Range] 3240 miles from French Polynesia"<< endl;
  77. }
  78. else if (shipPos <= 87.40) {
  79. std::cout << "Pacific (open waters) [Out of Transmission Range] 2160 miles from French Polynesia"<< endl;
  80. }
  81. else{
  82. std::cout << "Pacific (open waters) [Out of Transmission Range] 1080 miles from French Polynesia"<< endl;
  83. }
  84.  
  85. return;
  86.  
  87. }
  88.  
  89.  
  90.  
  91.  
  92.  
  93. //-----------------------------------------------
  94.  
  95.  
  96. //float satPos; //function 2 - view of 5 satellites
  97. //{
  98. //cin >> satPos;
  99.  
  100.  
  101.  
  102. //float interior; //function 3 - view of station interior
  103. //{
  104. //cin >> interior;

Main.cpp
  1. #include <iostream>
  2. #include "Look.h"
  3. using namespace std;
  4.  
  5. int choice;
  6.  
  7.  
  8.  
  9. int main()
  10. {
  11.  
  12.  
  13. std::cout << "________________________________"<< endl;
  14. std::cout << ""<< endl;
  15. std::cout << "Welcome to Platform XYZ-- Please Enter a Number:"<< endl;
  16. std::cout << ""<< endl;
  17. std::cout << "[1]Check Location for Transmission to Base"<< endl;
  18. std::cout << "[2]Stub"<< endl;
  19. std::cout << ""<< endl;
  20. std::cout << "________________________________"<< endl;
  21.  
  22.  
  23.  
  24. cin >> choice;
  25.  
  26. Look look1; //** ERROR?
  27.  
  28.  
  29. if(choice == 1){
  30. look1.printShipPos(); //** ERROR?
  31. }
  32. else {
  33. std::cout << "OTHER! chosen (stub)"<< endl;
  34. }
  35.  
  36.  
  37. system("PAUSE");
  38.  
  39. return 0;
  40. }

Error Message (VC++ Express 2005)
  1. Compiling...
  2. main.cpp
  3. Linking...
  4. main.obj : error LNK2019: unresolved external symbol "public: __thiscall Look::~Look(void)" (??1Look@@QAE@XZ) referenced in function _main
  5. main.obj : error LNK2019: unresolved external symbol "public: __thiscall Look::Look(void)" (??0Look@@QAE@XZ) referenced in function _main
  6. C:\Documents and Settings\RockStar\Desktop\Space_Station\spaceStation\Debug\spaceStation.exe : fatal error LNK1120: 2 unresolved externals
  7. Build log was saved at "file://c:\Documents and Settings\RockStar\Desktop\Space_Station\spaceStation\spaceStation\Debug\BuildLog.htm"
  8. spaceStation - 3 error(s), 0 warning(s)
  9. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Last edited by Ancient Dragon; Nov 1st, 2006 at 9:38 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Classes-- stuck in the mire of syntax

 
0
  #2
Oct 31st, 2006
Hmm... are all the files in the same folder ?
Have you checked the project settings of visual studio for this particular project ?
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Classes-- stuck in the mire of syntax

 
0
  #3
Oct 31st, 2006
Hi:

All files are in same folder.

As to the project settings, could you please elaborate? The project settings are still default (I am sure) as installed. I will look into this now.

Any suggestions (as to compiler\ project settings) are appreciated.

Thank-you for your reply.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,609
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 464
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: Classes-- stuck in the mire of syntax

 
0
  #4
Oct 31st, 2006
Okay, I will just like you to try out one thing if possible by you.

Create a new console C++ project in VS 2005. Create three blank files in it: Main.cpp, Look.cpp and Look.h

Copy and paste the contents of the original files into these files, check to see if all the new files created are in the same folder and same view.

I cant tell you the specifics of VS 2005 since its currently not installed on my computer .
I don't accept change; I don't deserve to live.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Classes-- stuck in the mire of syntax

 
0
  #5
Oct 31st, 2006
~s.o.s~:

TY for your reply.

I did as you suggested. All files are in the same project file.

I receive different errors now.

  1. Compiling...
  2. Look.cpp
  3. : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
  4.  
  5. Main.cpp
  6. : fatal error C1010: unexpected end of file while looking for precompiled header. Did you forget to add '#include "stdafx.h"' to your source?
  7. Generating Code...
  8.  
  9. floodland - 2 error(s), 0 warning(s)
  10. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
I do not know what to make of this: I have never knowingly used #include "stdafx.h" before;when I make this new project, files named this (#include "stdafx.h" ) were added, but I simply got rid of them and created a new .h file (Look.h) and two .cpp files (Look.cpp, Main.cpp) then cut-and-pasted the code in question.

I do not know what the error refers to exactly; I looked it up and saw it to be a standard include library (?), so I just added it to the other "include" files on each .cpp file.

Please let me know what you think if you get the chance.

Thanks alot,
Regards,

Arnold L.
Last edited by Ancient Dragon; Nov 1st, 2006 at 9:49 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Classes-- stuck in the mire of syntax

 
0
  #6
Oct 31st, 2006
Hi:

I made some adjustments to the compiler; I turned off the "precompiled headers" option (suggested on MSDN) and ran the build-- same, old error from before :

  1. ------ Build started: Project: floodland, Configuration: Debug Win32 ------
  2. Compiling...
  3. Look.cpp
  4. Main.cpp
  5. Generating Code...
  6. Compiling manifest to resources...
  7. Linking...
  8.  
  9. Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Look::~Look(void)" (??1Look@@QAE@XZ) referenced in function _main
  10.  
  11. Main.obj : error LNK2019: unresolved external symbol "public: __thiscall Look::Look(void)" (??0Look@@QAE@XZ) referenced in function _main
  12. floodland.exe : fatal error LNK1120: 2 unresolved externals
  13.  
  14. floodland - 3 error(s), 0 warning(s)
  15. ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Not sure what this means exactly. Will keep working it.
Last edited by Ancient Dragon; Nov 1st, 2006 at 9:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Classes-- stuck in the mire of syntax

 
1
  #7
Oct 31st, 2006
The reason you're getting those errors is because in your header file you defined Look::Look() and Look::~Look, and neglected to implement them in look.cpp.

The linker looks for the function code, can't find it, and spits out that error.
Last edited by John A; Oct 31st, 2006 at 10:12 pm.
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Classes-- stuck in the mire of syntax

 
0
  #8
Oct 31st, 2006
joeprogrammer:

TY for your reply. I appreciate you clarifying this. I am a bit confused at this point with classes in general; I have been working at this one area of the code for some time now and cannot get it to run. What you pointed out makes much better sense to me now. I am having trouble figuring out how to implement this in the Look.cpp file, though. The syntax is throwing me and all I keep getting is errors.

Look.cpp
  1. #include <iostream>
  2. #include "Look.h"
  3. using namespace std;
  4.  
  5.  
  6. void Look::printShipPos() //.h function set here, refers to class
  7. // Look via "::"
  8. {
  9.  
  10. cin >> shipPos; // this shipPos is the Look class member variable
  11.  
  12. if (shipPos <= 3.80) {
  13. std::cout << "French Polynesia [Out of Transmission Range]"<< endl;
  14. }
  15. else if (shipPos <= 7.60) {
  16. std::cout << "Maui, Hawaii [Out of Transmission Range]"<< endl;
  17. }
  18. else if (shipPos <= 11.40) {
  19. std::cout << "Pacific Ocean (open waters) [Out of Transmission Range] 4320 miles from Tampa, Florida"<< endl;
  20. }
  21. else if ((shipPos >= 15.20) && (shipPos < 19.00)) {
  22. std::cout << "Pacific Ocean (open waters) [In Transmission Range] 3240 miles from Tampa, Florida"<< endl;
  23. }
  24. else if ((shipPos >= 19.00) && (shipPos < 22.80)){
  25. std::cout << "La Paz, Baja, Mexico [In Transmission Range]"<< endl;
  26. }
  27. else if ((shipPos >= 22.80) && (shipPos < 26.60)){
  28. std::cout << "Gulf of Mexico [In Transmission Range]"<< endl;
  29. }
  30. else if ((shipPos >= 26.60) && (shipPos < 30.40)){
  31. std::cout << "Tampa, Florida [In Transmission Range]"<< endl;
  32. }
  33. else if ((shipPos >= 30.40) && (shipPos < 34.20)){
  34. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 4320 miles from Lisbon, Portugal"<< endl;
  35. }
  36. else if ((shipPos >= 34.20) && (shipPos < 38.00)){
  37. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 3240 miles from Lisbon, Portugal"<< endl;
  38. }
  39. else if (shipPos <= 38.00) {
  40. std::cout << "Atlantic Ocean (open waters) [In Transmission Range] 2160 miles from Lisbon, Portugal"<< endl;
  41. }
  42. else if (shipPos <= 41.80) {
  43. std::cout << "Canary Islands [Out of Transmission Range]"<< endl;
  44. }
  45. else if (shipPos <= 45.60) {
  46. std::cout << "Lisbon, Portugal [Out of Transmission Range]"<< endl;
  47. }
  48. else if (shipPos <= 49.40) {
  49. std::cout << "Tripoli, Libya [Out of Transmission Range]"<< endl;
  50. }
  51. else if (shipPos <= 53.20) {
  52. std::cout << "Baghdad, Iraq [Out of Transmission Range]"<< endl;
  53. }
  54. else if (shipPos <= 57.00) {
  55. std::cout << "Eastern Iran [Out of Transmission Range]"<< endl;
  56. }
  57. else if (shipPos <= 60.80) {
  58. std::cout << "Central Tajikistan [Out of Transmission Range]"<< endl;
  59. }
  60. else if (shipPos <= 64.60) {
  61. std::cout << "Kathmandu, Nepal [Out of Transmission Range]"<< endl;
  62. }
  63. else if (shipPos <= 68.40) {
  64. std::cout << "Hanoi, Vietnam [Out of Transmission Range]"<< endl;
  65. }
  66. else if (shipPos <= 72.20) {
  67. std::cout << "Hong Kong, China [Out of Transmission Range]"<< endl;
  68. }
  69. else if (shipPos <= 76.00) {
  70. std::cout << "Pacific (open waters) [Out of Transmission Range] 5400 miles from French Polynesia"<< endl;
  71. }
  72. else if (shipPos <= 79.80) {
  73. std::cout << "Pacific (open waters) [Out of Transmission Range] 4320 miles from French Polynesia"<< endl;
  74. }
  75. else if (shipPos <= 83.60) {
  76. std::cout << "Pacific (open waters) [Out of Transmission Range] 3240 miles from French Polynesia"<< endl;
  77. }
  78. else if (shipPos <= 87.40) {
  79. std::cout << "Pacific (open waters) [Out of Transmission Range] 2160 miles from French Polynesia"<< endl;
  80. }
  81. else{
  82. std::cout << "Pacific (open waters) [Out of Transmission Range] 1080 miles from French Polynesia"<< endl;
  83. }
  84.  
  85. return;
  86.  
  87. }
Thanks,
Arnold L.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 5,051
Reputation: John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold John A is a splendid one to behold 
Solved Threads: 332
Team Colleague
John A's Avatar
John A John A is offline Offline
Vampirical Lurker

Re: Classes-- stuck in the mire of syntax

 
0
  #9
Nov 1st, 2006
You have 2 options at this point:

You can choose to remove the constructor and destructor definitions from the class header. Classes are not required to have constructors/destructors.

You can add an implementation of these functions to the .cpp file. In this case, something like this would be all that you'd need:
  1. // look.cpp
  2. #include <iostream>
  3. #include "Look.h"
  4. using namespace std;
  5.  
  6. Look::Look() {
  7. }
  8.  
  9. Look::~Look() {
  10. }
  11. // ...
"Technological progress is like an axe in the hands of a pathological criminal."
Reply With Quote Quick reply to this message  
Join Date: Oct 2006
Posts: 2,564
Reputation: mattyd is an unknown quantity at this point 
Solved Threads: 1
Featured Poster
mattyd's Avatar
mattyd mattyd is offline Offline
Posting Maven

Re: Classes-- stuck in the mire of syntax

 
0
  #10
Nov 1st, 2006
joeprogrammer:

Thank-you for your reply. I tried your suggestion, adding

  1. Look::Look() {
  2. }
  3.  
  4. Look::~Look(){
  5. }

It worked great. All error messages vanquished!

I really apprciate your help.

Arnold L.
Last edited by Ancient Dragon; Nov 1st, 2006 at 9:47 pm. Reason: Removed extraneous formatting.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC