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:

g++ -Wall -g -I inc -I utils/include -c src/URL.cpp -o obj/URL.o
inc/URL.h:15: error: expected ‘,’ or ‘...’ before ‘&’ token
inc/URL.h:15: error: ISO C++ forbids declaration of ‘string’ with no type
inc/URL.h:27: error: expected ‘,’ or ‘...’ before ‘&’ token
inc/URL.h:27: error: ISO C++ forbids declaration of ‘string’ with no type
inc/URL.h:31: error: ‘string’ does not name a type
inc/URL.h:35: error: ‘string’ does not name a type
inc/URL.h:39: error: ‘string’ does not name a type
inc/URL.h:43: error: ‘string’ does not name a type
inc/URL.h:46: error: ‘ostream’ has not been declared
inc/URL.h:50: error: ‘string’ does not name a type
inc/URL.h:51: error: ‘string’ does not name a type
inc/URL.h:52: error: ‘string’ does not name a type
inc/URL.h:53: error: ‘string’ does not name a type
src/URL.cpp:13: error: expected ‘,’ or ‘...’ before ‘&’ token
src/URL.cpp:13: error: ISO C++ forbids declaration of ‘string’ with no type
src/URL.cpp: In constructor ‘URL::URL(int)’:
src/URL.cpp:15: error: ‘url’ was not declared in this scope
src/URL.cpp: In copy constructor ‘URL::URL(const URL&)’:
src/URL.cpp:21: error: ‘const class URL’ has no member named ‘GetURL’
src/URL.cpp: In member function ‘URL& URL::operator=(const URL&)’:
src/URL.cpp:33: error: ‘const class URL’ has no member named ‘GetURL’
src/URL.cpp: At global scope:
src/URL.cpp:38: error: expected ‘,’ or ‘...’ before ‘&’ token
src/URL.cpp:38: error: ISO C++ forbids declaration of ‘string’ with no type
src/URL.cpp: In member function ‘void URL::SetURL(int)’:
src/URL.cpp:40: error: ‘class URL’ has no member named ‘url’
src/URL.cpp:40: error: ‘url’ was not declared in this scope
src/URL.cpp: At global scope:
src/URL.cpp:46: error: ‘string’ does not name a type
src/URL.cpp:53: error: ‘string’ does not name a type
src/URL.cpp:60: error: ‘string’ does not name a type
src/URL.cpp:67: error: ‘string’ does not name a type
src/URL.cpp:73: error: ‘static’ may not be used when defining (as opposed to declaring) a static data member
src/URL.cpp:73: error: ‘bool URL::Test’ is not a static member of ‘class URL’
src/URL.cpp:73: error: ‘ostream’ was not declared in this scope
src/URL.cpp:73: error: ‘os’ was not declared in this scope
src/URL.cpp:74: error: expected ‘,’ or ‘;’ before ‘{’ token
src/URL.cpp: In member function ‘void URL::ParseURL()’:
src/URL.cpp:83: error: ‘url’ was not declared in this scope
src/URL.cpp:89: error: ‘url’ was not declared in this scope
src/URL.cpp:92: error: ‘prefix’ was not declared in this scope
src/URL.cpp: In member function ‘void URL::ParseHTTPURL()’:
src/URL.cpp:100: error: ‘url’ was not declared in this scope
src/URL.cpp:101: error: ‘InvalidURLExcpetion’ was not declared in this scope
src/URL.cpp:102: error: ‘scheme’ was not declared in this scope
src/URL.cpp:103: error: ‘url’ was not declared in this scope
src/URL.cpp:104: error: ‘root’ was not declared in this scope
src/URL.cpp: In member function ‘void URL::ParseFileURL()’:
src/URL.cpp:110: error: ‘url’ was not declared in this scope
src/URL.cpp:112: error: ‘scheme’ was not declared in this scope
src/URL.cpp:113: error: ‘root’ was not declared in this scope

URL.h:

#ifndef CS240_URL_H
#define CS240_URL_H

#include <string>
#include <ostream>

class URL
{
public:

	//!  No-arg constructor
	URL();

	//!  Constructor.  Creates a URL from string argument
	URL(const string & url);

	//!  Copy constructor.  Makes a complete copy of its argument
	URL(const URL & other);

	//!  Destructor
	~URL();

	//!  Assignment operator
	URL & operator =(const URL & other);

	//!  Sets the url to argument
	void SetURL(const string & url);

	//!  Returns the URL
	//!  @return URL
	string GetURL();

	//!  Returns the prefix for the URL (everything up until the file name)
	//!  @return prefix for URL
	string GetPrefix();

	//!  Returns the scheme for the URL (http or file)
	//!  @return scheme for URL
	string GetScheme();

	//!  Returns the root for the URL path
	//!  @return root for URL path
	string GetRoot();

	//!  Test function for URL class
	static bool Test(ostream & os);

private:

	string url;
	string prefix;
	string scheme;
	string root;

	//!  Parses the various parts of the URL
	void ParseURL();

	//!  Parses an HTTP URL
	void ParseHTTPURL();

	//!  Parses a file URL
	void ParseFileURL();
};

#endif

URL.cpp:

#include "URL.h"
#include <string>
#include "CS240Exception.h"
#include <ostream>
#include "UnitTest.h"

//!  No-arg constructor
URL::URL()
{
}

//!  Constructor.  Creates a URL from string argument
URL::URL(const string & url)
{
	SetURL(url);
}

//!  Copy constructor.  Makes a complete copy of its argument
URL::URL(const URL & other)
{
	SetURL(other.GetURL());
}

//!  Destructor
URL::~URL()
{
}

//!  Assignment operator
URL & URL::operator =(const URL & other)
{
	if (&other != this)
		SetURL(other.GetURL());
	return *this;
}

//!  Sets the url to argument
void URL::SetURL(const string & url)
{
	this->url = url;
	ParseURL();
}

//!  Returns the URL
//!  @return URL
string URL::GetURL()
{
	return url;
}

//!  Returns the prefix for the URL (everything up until the file name)
//!  @return prefix for URL
string URL::GetPrefix()
{
	return prefix;
}

//!  Returns the scheme for the URL (http or file)
//!  @return scheme for URL
string URL::GetScheme()
{
	return scheme;
}

//!  Returns the root for the URL path
//!  @return root for URL path
string URL::GetRoot()
{
	return root;
}

//!  Test function for URL class
static bool URL::Test(ostream & os)		// todo
{
	return true;
}

//!  Parses the various parts of the URL
void URL::ParseURL()
{
	int index;

	if (url.at(0) == 'h')
		ParseHTTPURL();
	else if (url.at(0) == 'f')
		ParseFileURL();
	else
		throw InvalidURLException("URL scheme is neither http nor file");
	index = url.find_last_of('#');
	url = url.substr(0, index);
	index = url.find_last_of('/');
	prefix = url.substr(0, index);
}

//!  Parses an HTTP URL
void URL::ParseHTTPURL()
{
	int index;

	if (url.substr(0, 7) != "http://")
		throw InvalidURLExcpetion("URL scheme is neither http nor file");
	scheme = "http";
	index = url.find_first_of('/', 7);
	root = url.substr(0, index);
}

//!  Parses a file URL
void URL::ParseFileURL()
{
	if (url.substr(0, 5) != "file:")
		throw InvalidURLException("URL scheme is neither http nor file");
	scheme = "file";
	root = "file:";
}

string is in the std namespace

string is in the std namespace

Thanks! using namespace std; along with a couple other quick corrections solved the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.