Thread: Help with errors on my code!

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

    Help with errors on my code!

    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;
    }
    The errors I got when i ran this on Microsoft Visual Studio professional 2010:
    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?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,633
    Start with the first error:
    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
    This error is on line 7 of histogram.h, I can't tell you the exact error because you did not post this file.

    Jim

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Indent your code!
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  4. #4
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    Actually histogram.h is in there in that mashup of 4 different files.

    Code:
    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;
    };
    Don't use histogram:: in your class member function definitions.

  5. #5
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    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;
    }

    now the only errors i am getting are the following:
    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>

    i know my array needs to be fixed im just not sure how exactly i would fix it

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    >>histogram::histogram(void)
    You can drop the void.

    Your add function is broken. You are trying to store data in a local array, but that array is destroyed once the function goes out of scope.
    You may also want to change your data pointer to a vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    15
    I have never worked with vectors, is there a way to fix my add function while still using an array? maybe something using const

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Well, that depends. First of all, you shouldn't use a local array - obviously. It will be destroyed when the function ends, along with all its data.
    Secondly, ask yourself: is it sufficient for the array to be static - that is, have a fixed size? Or does it have to be dynamic?
    If it's going to be dynamic, you are going to save yourself a lot of trouble by using std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-09-2010, 11:30 PM
  2. 2 errors in my code
    By johnnyg in forum C++ Programming
    Replies: 22
    Last Post: 05-04-2006, 08:04 AM
  3. Errors in code
    By itzme in forum C++ Programming
    Replies: 12
    Last Post: 01-07-2005, 05:11 PM
  4. strstr: code & errors
    By reRanger in forum C++ Programming
    Replies: 12
    Last Post: 11-23-2004, 05:03 PM
  5. Errors (not in code)
    By Da-Spit in forum C++ Programming
    Replies: 8
    Last Post: 05-18-2002, 12:31 AM