943,937 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 1795
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 31st, 2006
0

Classes-- stuck in the mire of syntax

Expand Post »
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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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
C++ Syntax (Toggle Plain Text)
  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)
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Oct 31st, 2006
0

Re: Classes-- stuck in the mire of syntax

Hmm... are all the files in the same folder ?
Have you checked the project settings of visual studio for this particular project ?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 31st, 2006
0

Re: Classes-- stuck in the mire of syntax

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.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Oct 31st, 2006
0

Re: Classes-- stuck in the mire of syntax

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 .
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006
Oct 31st, 2006
0

Re: Classes-- stuck in the mire of syntax

~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.

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Oct 31st, 2006
0

Re: Classes-- stuck in the mire of syntax

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 :

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Oct 31st, 2006
1

Re: Classes-- stuck in the mire of syntax

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.
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Oct 31st, 2006
0

Re: Classes-- stuck in the mire of syntax

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
C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006
Nov 1st, 2006
0

Re: Classes-- stuck in the mire of syntax

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. // ...
Team Colleague
Reputation Points: 2240
Solved Threads: 338
Vampirical Lurker
John A is offline Offline
5,055 posts
since Apr 2006
Nov 1st, 2006
0

Re: Classes-- stuck in the mire of syntax

joeprogrammer:

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

C++ Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 105
Solved Threads: 1
Posting Maven
mattyd is offline Offline
2,582 posts
since Oct 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Drag and drop of files between applications?
Next Thread in C++ Forum Timeline: Insertion Sort Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC