Thread: Help with error messages

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    15

    Help with error messages

    Code:
    //main.cpp
    #include "aRandomNumber.h"
    #include "histogram.h"
    
    int main()
    {
    	aRandomNumber die1();
    	histogram histogram1(die1.getRange());
    	for (int i=0; i<10; i++) //calls the add function 9000 times
    	{
    		histogram1.add(die1.generate());
    	}
    	histogram1.displayData();
    	histogram1.displayHistogram();
    
    	
    }
    Code:
    //histogram.h
    #pragma once
    class histogram
    {
    public:
    	histogram(void);
    	histogram(int numCount);
    	~histogram(void);
    	void histogram::add(int numGenerated);
    	void histogram::displayData();
    	void histogram::displayHistogram();
    	void histogram::add();
    
    private:
       int * data;
    	int low;
    	int range;
    };
    Code:
    //histogram.cpp
    #include "histogram.h"
    #include <iostream>
    #include <cstdlib>
    using namespace std; 
    
    histogram::histogram(void)
    {
    	data = new int[9];
    	for (int i = 0; i < 9; i++)
    	{
    		data[i] = 0;
    	}
    	low = 1;
    }
    histogram::histogram(int numCount)
    {
    	range = numCount;
    	data = new int[numCount];
    }
    
    void histogram::add(int numGenerated)
    {
    	int data[range];
    	int i;
    	for(i=0;data[i] != numGenerated; i++)
    	{
    	}
    	if (data[i] = numGenerated)
    	{
    		data[i]++;
    	}
    }
    void histogram::displayData()
    {
    	for(int i=0; i<range; i++)
    	{
    		cout << i + low << " " << data[i] << '\n' ;
    	}
    }
    
    void histogram::displayHistogram()
    {
    	// will need to include scaling factor here
    	for(int i=0; i<range; i++)
    	{
    		cout<< i+range;
    		
    		for(int stars=0; stars<data[i]; stars++)
    		{
    			cout<<'*';
    		}
    		cout<< endl;
    	}
    }
    	
    histogram::~histogram(void)
    {
    	delete[] data;
    }
    Code:
    //aRandomNumber.h
    #pragma once
    class aRandomNumber
    {
    public:
    	aRandomNumber(void); //default constructor
    	//~aRandomNumber(void); //default destructor 
    	aRandomNumber(int start, int end);
    	void aRandomNumber::setRange(int start, int end);
    	int aRandomNumber::generate();
    	int aRandomNumber::getRange();
    private:
    	int low;
    	int high;
    
    };
    Code:
    //aRandomNumber.cpp
    #include "aRandomNumber.h"
    #include <iostream>
    #include <cstdlib>
    using namespace std;
    
    
    aRandomNumber::aRandomNumber(void)
    {
    	low = 1;
    	high = 9;
    }
    aRandomNumber::aRandomNumber(int start, int end)
    {
    	low = start;
    	high = end;
    }
    
    int aRandomNumber::getRange()
    {
    	return (high - low + 1);
    }
    void aRandomNumber::setRange(int start, int end)
    {
    	low = start;
    	high = end;
    }
    int aRandomNumber::generate()
    {
    	int numGenerated; //holds the number generated by the random number function
    	numGenerated = low + rand() % getRange(); 
    	return numGenerated;
    }
    I got the following errors from Microsoft Visual Studio 2010 professional:
    main.cpp
    1>c:\users\ashley\documents\visual studio 2010\projects\random number project\random number project\main.cpp(8): error C2228: left of '.getRange' must have class/struct/union
    1>c:\users\ashley\documents\visual studio 2010\projects\random number project\random number project\main.cpp(11): error C2228: left of '.generate' must have class/struct/union
    1> histogram.cpp
    1>c:\users\ashley\documents\visual studio 2010\projects\random number project\random number project\histogram.cpp(24): error C2057: expected constant expression
    1>c:\users\ashley\documents\visual studio 2010\projects\random number project\random number project\histogram.cpp(24): error C2466: cannot allocate an array of constant size 0
    1>c:\users\ashley\documents\visual studio 2010\projects\random number project\random number project\histogram.cpp(24): error C2133: 'data' : unknown size
    1> Generating Code...

    what is wrong with my code
    Last edited by ashleyd; 10-30-2011 at 11:43 AM.

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    One problem is here:
    Code:
    aRandomNumber die1();
    This looks like a function definition to the compiler, rather than a variable declaration. Remove the empty brackets.

    Also, you can't do this in C++ where 'range' is a variable.
    Code:
    int data[range];
    Use a std::vector instead.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    This seems to be a follow on from Help with errors on my code!

    Better to keep it in one thread if you can

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error messages i need help please!
    By vodka9er in forum C++ Programming
    Replies: 2
    Last Post: 11-19-2007, 02:01 AM
  2. MANY MANY error messages
    By vrek in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2007, 10:58 PM
  3. error messages
    By lyojarhhs in forum C Programming
    Replies: 5
    Last Post: 03-27-2007, 02:31 AM
  4. Error messages
    By maxorator in forum Windows Programming
    Replies: 5
    Last Post: 11-08-2005, 02:24 PM
  5. need help with error messages
    By Shy_girl_311 in forum C++ Programming
    Replies: 0
    Last Post: 12-01-2001, 02:33 PM