Duncan
Duncan

Reputation: 3

error: expected type-specifier before ‘Number’

I have attempted to figure out the error above but have come nowhere. Every time I compiler I get the error:

/home/duncan/Desktop/OOPS/dac80/json/parser.cpp: In function ‘Value* parseString(std::stringstream&)’: /home/duncan/Desktop/OOPS/dac80/json/parser.cpp:149:19: error: expected type-specifier before ‘String’ Value* val = new String(name);

I have verified that I am including the correct header file in the source files so that the compiler recognizes the file. Below is the code concerning the error

Parser.cpp:

#include "object_model.h"

Value* parseString(std::stringstream& in)
{	
	std::string name("123");
	
	Value* val = new String(name);
	
	return val;
}

object_model.hpp:

#ifndef OBJECTMODEL_H
#define OBJECTMODEL_H

#include <string>
#include <sstream>
#include <map>
#include <vector>

enum ValueType { Object = 0, Array = 1, String = 2, Number = 3, True = 4, False = 5, Null = 6};


class Value
{
	public:
		Value() {}
		virtual ValueType getType() = 0;
};


class String : public Value
{
	public:
		String(std::string content);
		~String();
		
		std::string content;
		
		virtual ValueType getType();
};

#endif

object_model.cpp:

#include "object_model.h"

String::String(std::string content)
{
	this->content = content;
}

String::~String()
{

}

ValueType String::getType()
{
	return (ValueType)2;
}

Another thing that I have noticed if that I change String to Text then the code compiles completely. Not sure why but would the name String ever conflict with the std::string class?

Upvotes: 0

Views: 1235

Answers (1)

Mark Toller
Mark Toller

Reputation: 106

What Chris means when he says "No, it conflicts with your other String identifier" is that your 'class String' clashes with the identifier 'String' from "enum ValueType { Object = 0, Array = 1, String = 2, Number = 3, True = 4, False = 5, Null = 6};", so what the compiler sees for

Value* val = new String(name);

is

Value* val = new 2(name);

Upvotes: 2

Related Questions