Help with errors on my code

main.cpp:
#include "aRandomNumber.h"
#include "histogram.h"

int main()
{
aRandomNumber die1 = new aRandomNumber();
histogram histogram1 = new histogram();
for (int i=0; i<10; i++) //calls the add function 10 times
{
histogram1.add(die1.generate());
}
histogram1.displayData();
histogram1.displayHistogram();
}
aRandomNumber.h
#pragma once
class aRandomNumber
{
public:
aRandomNumber(void); //default constructor
aRandomNumber(int start, int end);
void aRandomNumber::setRange(int start, int end);
int aRandomNumber::generate();
int aRandomNumber::getRange();
private:
int low;
int high;

};
aRandomNumber.cpp
#include "aRandomNumber.h"
#include <iostream>
#include <cstdlib>



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;
}
histogram.h
#pragma once
class histogram
{
public:
histogram(void);
~histogram(void);
void histogram::add(int numGenerated);
void histogram::displayData();
void histogram::displayHistogram();
void histogram::add();

private:
int * data;
int low;
};
histogram.cpp
#include "histogram.h"
#include <iostream>
#include <cstdlib>


histogram::histogram(void)
{
data = new int[9];
for (int i = 0; i < 9; i++)
{
*data[i] = 0;
}
low = 1
}
histogram::histogram(int numCount)
{
data = new int[numCount];
}

void histogram::add(int numGenerated)
{
int data[range]
for(int 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;
}


When I debug this code on Microsoft visual studio i get the following errors:
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(7): warning C4183: 'printHistogram': missing return type; assumed to be a member function returning 'int'
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(8): warning C4183: 'printHistogram': missing return type; assumed to be a member function returning 'int'
1> histogram.cpp
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(7): warning C4183: 'printHistogram': missing return type; assumed to be a member function returning 'int'
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(8): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(8): warning C4183: 'printHistogram': missing return type; assumed to be a member function returning 'int'
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(9): error C2511: 'histogram::histogram(int)' : overloaded member function not found in 'histogram'
1> c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.h(3) : see declaration of 'histogram'
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(16): error C2057: expected constant expression
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(16): error C2466: cannot allocate an array of constant size 0
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(16): error C2440: 'initializing' : cannot convert from 'int' to 'int []'
1> There are no conversions to array types, although there are conversions to references or pointers to arrays
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(20): error C3861: 'generate': identifier not found
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(21): error C2065: 'low' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(27): error C2065: 'i' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(29): error C2065: 'i' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(35): error C2065: 'range' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(37): error C2065: 'cout' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(37): error C2065: 'low' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(44): error C2065: 'range' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(46): error C2065: 'cout' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(46): error C2065: 'range' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(50): error C2065: 'cout' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(52): error C2065: 'cout' : undeclared identifier
1>c:\users\ashley\documents\visual studio 2010\projects\project1\project1\histogram.cpp(52): error C2065: 'endl' : undeclared identifier
1> Generating Code...
1>
1>Build FAILED



What is wrong with my code? what is causing these errors? and how do i fix it?
The first thing you need to do is edit your above post so it's (a) readable and (b) we can see which line is 46 without counting! See "How to use tags"
http://www.cplusplus.com/articles/z13hAqkS/

Then you need to NOT repeat the class name for every method declared in the class.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
class histogram
{
public:
    histogram(void);
    ~histogram(void);
    void add(int numGenerated); // not histogram::add(...
    void displayData();
    void displayHistogram();
    void add();

private:
    int * data;
    int low;
};


And you need to add using namespace std; just after your #includes in histogram.cpp

Plus I think you've got a couple of undeclared variables, but it's difficult to spot amongst all the other errors.

Last edited on
Topic archived. No new replies allowed.