943,712 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 4238
  • C++ RSS
Mar 18th, 2008
0

Unknown syntax error in C++ class

Expand Post »
I am getting compiler output that makes me think I have some sort of syntax error like a missing ';' or misaligned (). But I've checked my code thoroughly and can't find anything wrong. What else could cause this kind of error? I included the compiler output along with my .h and .cpp file for the class that won't compile. The other header files I #include shouldn't be causing the problem as they've already been thoroughly tested.

Compiler output:
C++ Syntax (Toggle Plain Text)
  1. g++ -Wall -g -I inc -I utils/include -c src/URL.cpp -o obj/URL.o
  2. inc/URL.h:15: error: expected ‘,’ or ‘...’ before ‘&’ token
  3. inc/URL.h:15: error: ISO C++ forbids declaration of ‘string’ with no type
  4. inc/URL.h:27: error: expected ‘,’ or ‘...’ before ‘&’ token
  5. inc/URL.h:27: error: ISO C++ forbids declaration of ‘string’ with no type
  6. inc/URL.h:31: error: ‘string’ does not name a type
  7. inc/URL.h:35: error: ‘string’ does not name a type
  8. inc/URL.h:39: error: ‘string’ does not name a type
  9. inc/URL.h:43: error: ‘string’ does not name a type
  10. inc/URL.h:46: error: ‘ostream’ has not been declared
  11. inc/URL.h:50: error: ‘string’ does not name a type
  12. inc/URL.h:51: error: ‘string’ does not name a type
  13. inc/URL.h:52: error: ‘string’ does not name a type
  14. inc/URL.h:53: error: ‘string’ does not name a type
  15. src/URL.cpp:13: error: expected ‘,’ or ‘...’ before ‘&’ token
  16. src/URL.cpp:13: error: ISO C++ forbids declaration of ‘string’ with no type
  17. src/URL.cpp: In constructor ‘URL::URL(int)’:
  18. src/URL.cpp:15: error: ‘url’ was not declared in this scope
  19. src/URL.cpp: In copy constructor ‘URL::URL(const URL&)’:
  20. src/URL.cpp:21: error: ‘const class URL’ has no member named ‘GetURL’
  21. src/URL.cpp: In member function ‘URL& URL::operator=(const URL&)’:
  22. src/URL.cpp:33: error: ‘const class URL’ has no member named ‘GetURL’
  23. src/URL.cpp: At global scope:
  24. src/URL.cpp:38: error: expected ‘,’ or ‘...’ before ‘&’ token
  25. src/URL.cpp:38: error: ISO C++ forbids declaration of ‘string’ with no type
  26. src/URL.cpp: In member function ‘void URL::SetURL(int)’:
  27. src/URL.cpp:40: error: ‘class URL’ has no member named ‘url’
  28. src/URL.cpp:40: error: ‘url’ was not declared in this scope
  29. src/URL.cpp: At global scope:
  30. src/URL.cpp:46: error: ‘string’ does not name a type
  31. src/URL.cpp:53: error: ‘string’ does not name a type
  32. src/URL.cpp:60: error: ‘string’ does not name a type
  33. src/URL.cpp:67: error: ‘string’ does not name a type
  34. src/URL.cpp:73: error: ‘static’ may not be used when defining (as opposed to declaring) a static data member
  35. src/URL.cpp:73: error: ‘bool URL::Test’ is not a static member of ‘class URL’
  36. src/URL.cpp:73: error: ‘ostream’ was not declared in this scope
  37. src/URL.cpp:73: error: ‘os’ was not declared in this scope
  38. src/URL.cpp:74: error: expected ‘,’ or ‘;’ before ‘{’ token
  39. src/URL.cpp: In member function ‘void URL::ParseURL()’:
  40. src/URL.cpp:83: error: ‘url’ was not declared in this scope
  41. src/URL.cpp:89: error: ‘url’ was not declared in this scope
  42. src/URL.cpp:92: error: ‘prefix’ was not declared in this scope
  43. src/URL.cpp: In member function ‘void URL::ParseHTTPURL()’:
  44. src/URL.cpp:100: error: ‘url’ was not declared in this scope
  45. src/URL.cpp:101: error: ‘InvalidURLExcpetion’ was not declared in this scope
  46. src/URL.cpp:102: error: ‘scheme’ was not declared in this scope
  47. src/URL.cpp:103: error: ‘url’ was not declared in this scope
  48. src/URL.cpp:104: error: ‘root’ was not declared in this scope
  49. src/URL.cpp: In member function ‘void URL::ParseFileURL()’:
  50. src/URL.cpp:110: error: ‘url’ was not declared in this scope
  51. src/URL.cpp:112: error: ‘scheme’ was not declared in this scope
  52. src/URL.cpp:113: error: ‘root’ was not declared in this scope

URL.h:
c++ Syntax (Toggle Plain Text)
  1. #ifndef CS240_URL_H
  2. #define CS240_URL_H
  3.  
  4. #include <string>
  5. #include <ostream>
  6.  
  7. class URL
  8. {
  9. public:
  10.  
  11. //! No-arg constructor
  12. URL();
  13.  
  14. //! Constructor. Creates a URL from string argument
  15. URL(const string & url);
  16.  
  17. //! Copy constructor. Makes a complete copy of its argument
  18. URL(const URL & other);
  19.  
  20. //! Destructor
  21. ~URL();
  22.  
  23. //! Assignment operator
  24. URL & operator =(const URL & other);
  25.  
  26. //! Sets the url to argument
  27. void SetURL(const string & url);
  28.  
  29. //! Returns the URL
  30. //! @return URL
  31. string GetURL();
  32.  
  33. //! Returns the prefix for the URL (everything up until the file name)
  34. //! @return prefix for URL
  35. string GetPrefix();
  36.  
  37. //! Returns the scheme for the URL (http or file)
  38. //! @return scheme for URL
  39. string GetScheme();
  40.  
  41. //! Returns the root for the URL path
  42. //! @return root for URL path
  43. string GetRoot();
  44.  
  45. //! Test function for URL class
  46. static bool Test(ostream & os);
  47.  
  48. private:
  49.  
  50. string url;
  51. string prefix;
  52. string scheme;
  53. string root;
  54.  
  55. //! Parses the various parts of the URL
  56. void ParseURL();
  57.  
  58. //! Parses an HTTP URL
  59. void ParseHTTPURL();
  60.  
  61. //! Parses a file URL
  62. void ParseFileURL();
  63. };
  64.  
  65. #endif

URL.cpp:
c++ Syntax (Toggle Plain Text)
  1. #include "URL.h"
  2. #include <string>
  3. #include "CS240Exception.h"
  4. #include <ostream>
  5. #include "UnitTest.h"
  6.  
  7. //! No-arg constructor
  8. URL::URL()
  9. {
  10. }
  11.  
  12. //! Constructor. Creates a URL from string argument
  13. URL::URL(const string & url)
  14. {
  15. SetURL(url);
  16. }
  17.  
  18. //! Copy constructor. Makes a complete copy of its argument
  19. URL::URL(const URL & other)
  20. {
  21. SetURL(other.GetURL());
  22. }
  23.  
  24. //! Destructor
  25. URL::~URL()
  26. {
  27. }
  28.  
  29. //! Assignment operator
  30. URL & URL::operator =(const URL & other)
  31. {
  32. if (&other != this)
  33. SetURL(other.GetURL());
  34. return *this;
  35. }
  36.  
  37. //! Sets the url to argument
  38. void URL::SetURL(const string & url)
  39. {
  40. this->url = url;
  41. ParseURL();
  42. }
  43.  
  44. //! Returns the URL
  45. //! @return URL
  46. string URL::GetURL()
  47. {
  48. return url;
  49. }
  50.  
  51. //! Returns the prefix for the URL (everything up until the file name)
  52. //! @return prefix for URL
  53. string URL::GetPrefix()
  54. {
  55. return prefix;
  56. }
  57.  
  58. //! Returns the scheme for the URL (http or file)
  59. //! @return scheme for URL
  60. string URL::GetScheme()
  61. {
  62. return scheme;
  63. }
  64.  
  65. //! Returns the root for the URL path
  66. //! @return root for URL path
  67. string URL::GetRoot()
  68. {
  69. return root;
  70. }
  71.  
  72. //! Test function for URL class
  73. static bool URL::Test(ostream & os) // todo
  74. {
  75. return true;
  76. }
  77.  
  78. //! Parses the various parts of the URL
  79. void URL::ParseURL()
  80. {
  81. int index;
  82.  
  83. if (url.at(0) == 'h')
  84. ParseHTTPURL();
  85. else if (url.at(0) == 'f')
  86. ParseFileURL();
  87. else
  88. throw InvalidURLException("URL scheme is neither http nor file");
  89. index = url.find_last_of('#');
  90. url = url.substr(0, index);
  91. index = url.find_last_of('/');
  92. prefix = url.substr(0, index);
  93. }
  94.  
  95. //! Parses an HTTP URL
  96. void URL::ParseHTTPURL()
  97. {
  98. int index;
  99.  
  100. if (url.substr(0, 7) != "http://")
  101. throw InvalidURLExcpetion("URL scheme is neither http nor file");
  102. scheme = "http";
  103. index = url.find_first_of('/', 7);
  104. root = url.substr(0, index);
  105. }
  106.  
  107. //! Parses a file URL
  108. void URL::ParseFileURL()
  109. {
  110. if (url.substr(0, 5) != "file:")
  111. throw InvalidURLException("URL scheme is neither http nor file");
  112. scheme = "file";
  113. root = "file:";
  114. }
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dgr81 is offline Offline
2 posts
since Mar 2008
Mar 18th, 2008
0

Re: Unknown syntax error in C++ class

string is in the std namespace
Reputation Points: 53
Solved Threads: 33
Posting Whiz in Training
bugmenot is offline Offline
224 posts
since Nov 2006
Mar 18th, 2008
0

Re: Unknown syntax error in C++ class

Click to Expand / Collapse  Quote originally posted by bugmenot ...
string is in the std namespace
Thanks! using namespace std; along with a couple other quick corrections solved the problem.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
dgr81 is offline Offline
2 posts
since Mar 2008

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: Linked Lists Database...
Next Thread in C++ Forum Timeline: time complexity problem





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


Follow us on Twitter


© 2011 DaniWeb® LLC