New to C++, 'Error: expected unqualified-id before '{' token'.

About my program.
Hi, first year uni student in the UK, I'm new to posting on forums, but I thought I'd give it a shot because C++ has been frustrating me lately.

My program is a traffic lights system that should be able to switch between different modes automatically depending on different circumstances i.e input from a photo resistor and a range finder. I've written this flawlessly using functions, but I have decided to re-write the program to be more object oriented to meet my Uni's criteria.

I've only ever really dabbled with python, which I started to use at the start of my first year at Uni, so I don't have much experience programming, I can work my way around python though. I picked up C++ for this project around 2 to 3 days ago, and Classes have taken it out of me, I can hardly work with them even though I understand and have used them in python regularly (the idea's the same I think?) I've looked around for resources on the web and asked my lecturer for resources on this topic too, however they haven't been much help.

From what I gather I have a syntax error, (as well as a general organisation and functionality one - besides the point). If anyone could give me guidance, I'd very much appreciate it. Thanks in advance.

The Error

I'm using Tinkercad to emulate an Arduino (Uno R3), here Is the error I'm receiving.

20:1: error: expected unqualified-id before '{' token
50:1: error: expected unqualified-id before '{' token
68:1: error: expected unqualified-id before '{' token
125:1: error: expected unqualified-id before '{' token
 In function 'void loop()':
164:7: error: expected unqualified-id before '.' token
 exit status 1

The Code
Here is the code I have written using Sublime.

// Class Names.

#define rangeFinder rf;
#define lights light;
#define photoResistor resistor;
#define trafficLights check;

// Main Class Definitions.

class trafficLights
{
public:
	void normal()
	{
		if (resistor.mode = 'over' && rangeFinder.distance > 10)
		{
			light.red('normal');
			light.yellow('normal');
			light.green('normal');
			light.yellow('normal');
		}
		if (resistor.mode = 'over' && rangeFinder.distance < 10)
		{
			tone(5, 261);
			light.yellow('blink');
			noTone(5);
		}
		if (resistor.mode = 'under' && rangeFinder.distance > 10)
		{
			light.red('on');
		}
		if (resistor.mode = 'under' && rangeFinder.distance < 10)
		{
			light.allOff();
			light.green('on');
		}
	}
};

class photoResistor
{
public:
	char input = A0;
	char mode{getLightIntensity(input)};

	char getLightIntensity(input)
	{
		int reading = analogRead(input);
		if (reading > 50)
		{
			return 'over';
		} else {
			return 'under';
		}
	}
};

class lights
{
public:
	void red(char mode)
	{
		switch(mode){
			case 'on':
				digitalWrite(2, HIGH);
			case 'off':
				digitalWrite(2, LOW);
			case 'normal':
				digitalWrite(2, HIGH);
				delay(2000);
				digitalWrite(2, LOW);
		}
	}
	void yellow(char mode)
	{
		switch(mode){
			case 'on':
				digitalWrite(3, HIGH);
			case 'off':
				digitalWrite(3, LOW);
			case 'normal':
				digitalWrite(3, HIGH);
				delay(1000);
				digitalWrite(3, LOW);
			case 'blink':
				for (int x = 0; x <= 10; x += 1){
					digitalWrite(3, HIGH);
					delay(100);
					digitalWrite(3, LOW);
					delay(100);
				}
		}
	}
	void green(char mode)
	{
		switch(mode){
			case 'on':
				digitalWrite(2, HIGH);
			case 'off':
				digitalWrite(2, LOW);
			case 'normal':
				digitalWrite(2, HIGH);
				delay(2000);
				digitalWrite(2, LOW);
		}
	}
	void allOff()
	{
		digitalWrite(2, LOW);
		digitalWrite(3, LOW);
		digitalWrite(4, LOW);
	}
};

class rangeFinder
{
public:
	
	int trig = 7, echo = 6;
	int distance{returnDistance()};

	void reset()
	{
		digitalWrite(trig, LOW);
		delay(2 / 1000);
		digitalWrite(trig, HIGH);
		delay(10 / 1000);
		digitalWrite(trig, LOW);
	}
	int returnDistance()
	{
		reset();
		int timeTaken = pulseIn(echo, HIGH);
		return distance = timeTaken * 0.034 / 2;
	}
};

// Setup.

void setup()
{
	pinMode(2, OUTPUT);
	pinMode(3, OUTPUT);
	pinMode(4, OUTPUT);
	pinMode(5, OUTPUT);
	pinMode(6, INPUT);
	pinMode(7, OUTPUT);
	pinMode('A0', INPUT);
}

// Loop.

void loop()
{
	check.normal;
	delay(10);
}

Final Comments
I'd appreciate any help I can get, I've been stuck on this issue for quite a while now and it is frustrating knowing that it's most likely me being blind.

Thanks!

'over'

Single quotes for single characters, double quotes for strings.
So "over" and "under" and "normal" and etc.

pinMode('A0', INPUT);
There are no quotes for the A0 pin.

#define rangeFinder rf;

Semicolons at the end of preprocessor macros are almost always a bad idea.

pinMode('A0', INPUT);Lose the quotes.

Update
So I have updated my code, merging the classes into one and fixed most of the errors that appeared after removing the '#defines' as they weren't what I expected them to be, fixing some my if statements and also changing the single quotes to doubles as it should be, thanks for pointing that out. Also removed the single quotes around the A0 as mentioned, here is what I am left with now.

Code

// Main Class Definitions.

class trafficLights
{
public:

	class rangeFinder
	{
	public:

		int trig = 7, echo = 6;
		int distance{returnDistance()};

		void reset()
		{
			digitalWrite(trig, LOW);
			delay(2 / 1000);
			digitalWrite(trig, HIGH);
			delay(10 / 1000);
			digitalWrite(trig, LOW);
		}
		int returnDistance()
		{
			reset();
			int timeTaken = pulseIn(echo, HIGH);
			return distance = timeTaken * 0.034 / 2;
		}
	};

	class photoResistor
	{
	public:
		const char* mode{getLightIntensity()};

		const char* getLightIntensity()
		{
			int reading = analogRead(A0);
			if (reading > 50)
			{
				return "over";
			} else {
				return "under";
			}
		}
	};

		class lights
		{
		public:
			void red(int mode)
			{
				switch(mode){
					case 1:
						digitalWrite(2, HIGH);
					case 0:
						digitalWrite(2, LOW);
					case 2:
						digitalWrite(2, HIGH);
						delay(2000);
						digitalWrite(2, LOW);
				}
			}
			void yellow(int mode)
			{
				switch(mode){
					case 1:
						digitalWrite(3, HIGH);
					case 0:
						digitalWrite(3, LOW);
					case 2:
						digitalWrite(3, HIGH);
						delay(1000);
						digitalWrite(3, LOW);
					case 3:
						for (int x = 0; x <= 10; x += 1){
							digitalWrite(3, HIGH);
							delay(100);
							digitalWrite(3, LOW);
							delay(100);
						}
				}
			}
			void green(int mode)
			{
				switch(mode){
					case 1:
						digitalWrite(2, HIGH);
					case 0:
						digitalWrite(2, LOW);
					case 2:
						digitalWrite(2, HIGH);
						delay(2000);
						digitalWrite(2, LOW);
				}
			}
			void allOff()
			{
				digitalWrite(2, LOW);
				digitalWrite(3, LOW);
				digitalWrite(4, LOW);
			}
	};

	rangeFinder rf;
	lights light;
	photoResistor resistor;

	void normal()
	{
		if (resistor.mode == "over" && rangeFinder.distance > 10)
		{
			light.red(2);
			light.yellow(2);
			light.green(2);
			light.yellow(2);
		}
		if (resistor.mode == "over" && rangeFinder.distance < 10)
		{
			tone(5, 261);
			light.yellow(3);
			noTone(5);
		}
		if (resistor.mode == "under" && rangeFinder.distance > 10)
		{
          	light.allOff();
			light.red(1);
		}
		if (resistor.mode == "under" && rangeFinder.distance < 10)
		{
			light.allOff();
			light.green(1);
		}
	}
};

// Setup.

void setup()
{
	pinMode(2, OUTPUT);
	pinMode(3, OUTPUT);
	pinMode(4, OUTPUT);
	pinMode(5, OUTPUT);
	pinMode(6, INPUT);
	pinMode(7, OUTPUT);
	pinMode(A0, INPUT);
}

// Loop.

trafficLights tf;

void loop()
{
	tf.normal;
	delay(10);
}

New Error

The error I'm receiving now, is the following:

 In member function 'void trafficLights::normal()':
120:45: error: expected primary-expression before '.' token
127:45: error: expected primary-expression before '.' token
133:46: error: expected primary-expression before '.' token
138:46: error: expected primary-expression before '.' token
 In function 'void loop()':
165:5: error: invalid use of non-static member function 'void trafficLights::normal()'
118:7: note: declared here
 exit status 1

I understand it is to do with the normal() function in the trafficLights class, however what I do not understand is the non-static member part, could anyone help further? Thanks.

P.S
I appreciate the help so far!

And this:

if (resistor.mode = 'over' && rangeFinder.distance > 10)

is attempting to ASSIGN 'over' to resistor.mode, NOT comparing. = is assignment. == is comparison. But if resistor.mode is a char[], == will not work. You must use strcmp() or similar.

...but 'over' isn't a string, so you can't use strcmp.
"over" is a string (double quotes)

I wouldn't have started with nested classes personally!

You have a rangeFinder called rf, yet you try to call methods on rangeFinder not on rf

You can't compare C-strings using ==, use strcmp.

You forgot the parentheses in the call to tf.normal in loop()

I'd have used an enumeration or #defined constants rather than C-string for the
result of getLightIntensity - string isn't a friendly type for sematic use, its for
talking to humans.

Why does getLightIntensity not have an option for correct intensity, only over or under?

And most importantly, class names should have a capital letter at the start by convention,
you'll confuse so many people if you don't stick to this....

Thanks for the help.
Thanks for the help. I did figure out the problems eventually myself, I forgot to check back on here. Thanks again anyway.

For clarification to one of the questions regarding light intensity, all I need is to know whether the light intensity is over or under a certain limit, which it does alright I guess.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.