Jump to content
  • Advertisement
Sign in to follow this  

error C2228: left of '.Train' must have class/struct/union type

This topic is 4702 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

This is an error I dont understand, Im trying to make the simpliest neural net imageinable, two inputs, nothing hidden and the aim is for it to learn the XOR function. But somehow, it doesnt like my neuron class. When I try to train it, I get the error message in topic, why? The train function belongs to the Neuron class so it should be ok. Main.cpp
#include <iostream>
#include <vector>
#include <conio.h>

#include "Neuron.h"

using namespace std;

#define TRAINTIMES 5000

int main (void) {
	Neuron Net;

	for(int i = 0; i < TRAINTIMES; i++) {
		cout << "(1, 1) --- " << Net.Train(1, 1, 0) << endl;
		cout << "(1, 0) --- " << Net.Train(1, 0, 1) << endl;
		cout << "(0, 1) --- " << Net.Train(0, 1, 1) << endl;
		cout << "(0, 0) --- " << Net.Train(0, 0, 0) << endl;

	return 1;

#include <vector>
#include <stdlib.h>
#include <time.h>
#include <iostream>

#define LEARNRATE 0.01

using namespace std;

class Neuron {
	float w1;
	float w2;
	float wb;	// bias

	int hardlimiter(int);
	int inputsTotal;

			Neuron();	// ctor
	float	Train(int i1, int i2, int correctOutput);

#include "Neuron.h"

Neuron::Neuron() {
	w1 = (((rand()%2000)-1000)/1000);
	w2 = (((rand()%2000)-1000)/1000);
	wb = (((rand()%2000)-1000)/1000); // bias

float Neuron::Train(int i1, int i2, int correctOutput) {
	float output = (i1*w1) + (i2*w2) + (1*wb);	
	output = hardlimiter(output);	// sum up inputs and weights

	// training!

	float error = correctOutput - output;
	//cout << error << endl;

	w1 = w1+(LEARNRATE*error*i1);
	w2 = w2+(LEARNRATE*error*i2);
	wb = wb+(LEARNRATE*error*1);

	return output;

int Neuron::hardlimiter(int a) {
	if(a < 0)
		return 0;
		return 1;

[Edited by - Mizipzor on January 4, 2006 8:17:39 AM]

Share this post

Link to post
Share on other sites
Thanks, didnt know classes had to be created like that if they ctor didnt take any arguments. :)

But I got another problem now, something is severly wrong. It doesnt learn XOR, it learns the complete opposite. :P

1 1 = 1
1 0 = 0
0 1 = 0
0 0 = 1

Ive updated my first post with the current source, can anyone see the cause of this problem?

Share this post

Link to post
Share on other sites
I haven't gone through all the code, but why don't you just use the xor operator?

1 ^ 1 = 0
1 ^ 0 = 1
0 ^ 1 = 1
0 ^ 0 = 0

Share this post

Link to post
Share on other sites
Original post by Mizipzor
The point was more to make a working neural net, not get the result of a xor operation.

Ah well, I guess I misunderstood the issue.
Unfortunately, my limited knowledge of NN prevents me from being of any assistance. [smile]

Share this post

Link to post
Share on other sites
Bummer. Well at least you tried. [smile]

Ive tried changing the learning rate and the number of times the net is trained but nothing seems to help. The training function really isnt long, but maybe I should have posted in the artificial intelligence section if I wanted anyone good with AI to take a look at it.

Maybe I should change the topic to... :/ hmm

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!