Thread: I'm about to give up!

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    6

    I'm about to give up!

    Hello!

    This is my first post here. After having spent countless hours on forums with a gazillion tabs open trying to find a solution to my problem, I feel that I now have no other choice but to ask for your help.

    What really bugs me is that this is supposed to be really easy to complete, but I just can't wrap my head around it! This has taken me far too long.

    The code consists of one .h file and three cpp files.

    This is the .h file:

    Code:
    
    
    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    usingnamespacestd;
    
    
    class heltal 
    {
    private:
        int tal; 
    public:
        heltal();
        heltal(int nmr); 
        void setValue(int value, int nmr);
        int getValue() const; 
        heltal operator< (heltal &heltal); 
        heltal operator> (heltal &heltal); 
    };
    
    
    class Array             
    {
    private:
    heltal *ptrHeltal;    
        int size;              
    
    public:
        Array();                
        Array(int arraysize);   
        ~Array();               
    
        int generate_numbers(int arraysize, int high, int low)    
        {
            int i;       
    
            srand((unsigned)time(0));    
    
    cout << "\n\nThe generated numbers are:\n";
    
    
            
    for (i = 0; i <= arraysize; i++)
            {
    ptrHeltal[i] = (rand() % high + low);  
    
                cout << "\n" << ptrHeltal[i];
    
            }
    
    
    
    
            return 0;
        }
    
    
    };
    
    
    



    This is a cpp file called "array.cpp":


    Code:
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    #include "heltal.h"
    
    
    
    
    usingnamespacestd;
    
    
    
    Array::Array()
    {
        size=50;                        
    ptrHeltal = newheltal[size];   
        
        
    }
    
    
    Array::Array(int arraystorlek)  
    {
        
        size = arraystorlek;            
    ptrHeltal = newheltal[size];   
        
    }
    
    
    Array::~Array()
    {
        
        delete []ptrHeltal; 
        
    }

    This is another cpp file called "heltal.cpp":



    Code:
    #include <iostream>
    #include "heltal.h"
    usingnamespacestd;
    
    
    
    
    heltal::heltal()
    {
    tal = 0;        
    }
    
    
    heltal::heltal(int nmr)
    {
        nmr = tal;         
    }
    
    
    void heltal::setValue(int value, int nmr)
    {
        
        
        nmr = value;            
    }
    
    
    
    
    int heltal::getValue() const
    {
        return tal;             
    }
    
    
    
    
    heltaloperator< (heltal tal, heltal value)    
    {
        tal = value;
        
        return tal;
        
    }
    
    
    heltaloperator> (heltal tal, heltal value)     
    {
        value = tal;
        
        return value;
    }



    And lastly, this is the "main.cpp":


    Code:
    
    #include <iostream>
    #include "heltal.h"
    usingnamespacestd;
    
    
    
    int main()
    {
        
        int arraysize;         
        int high;               
        int low;                
        
    cout << "How large should the array be?" << endl;       
        cin >> arraysize;       
        
        
    cout << "What should the lowest number be?" << endl;   
        cin >> low;             
        
    cout << "What should the highest number be?" << endl;   
        cin >> high;           
        
        
    Array run(arraysize);       
        
        run.generate_numbers(arraysize, high, low);         
        
        
        
        
        return 0;
    }
    Code:
    
    



    And now to my issue. In the "heltal.h" function generate_numbers, I can't seem to print the array "heltal[i]". I get this error saying:

    "Invalid operands to binary expression ('basic_ostream<char, std::__1::char_traits<char>>' and 'heltal')

    What in the world is the meaning of this? Is the problem somewhere else in my code or should I print the array in a different way?

    I am absolutely hopelessly clueless and would be immensely happy if anyone could just give me a hint as to why I'm failing!


    For those of you who have read this long thread, thank you!

  2. #2
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    you haven't defined a way for std:: ostream to print a heltal object. you need to define a function like this:

    Code:
    std::ostream& operator << (std::ostream& os, const heltal& rhs)
    {
      // the code to print the object goes here
      return os;
    }

  3. #3
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by Elkvis View Post
    you haven't defined a way for std:: ostream to print a heltal object. you need to define a function like this:

    Code:
    std::ostream& operator << (std::ostream& os, const heltal& rhs)
    {
      // the code to print the object goes here
      return os;
    }

    Thank you for your answer! However, I do have one or two follow-up questions.
    When I type this, I get an error saying "Overloaded 'operator <<' must be a binary operator (has 3 parameters)". Thoughts on this?
    Also, I'm not entirely sure as to where this piece of code should be placed?

    Thanks again!

  4. #4
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    post your code please.

  5. #5
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by Elkvis View Post
    post your code please.



    Not sure I follow you.. I have no other code than what is posted in the first post. If you're referring to the code that's supposed to be in what you posted I have no code for it. To be honest I don't even know where to put it..

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    it should go somewhere after the class definition of heltal, but before the definition of generate_numbers, and must be in the global scope in your program, not inside a class.

  7. #7
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    Place it in the .h

    make a try
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  8. #8
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by Elkvis View Post
    it should go somewhere after the class definition of heltal, but before the definition of generate_numbers, and must be in the global scope in your program, not inside a class.




    Alright, so basically what I did was to put it in between the two classes. This took care of the error codes I got! However, I tried running it, and got an error saying:

    "ld: 2 duplicate symbols for architecture x86_64clang: error: linker command failed with exit code 1 (use -v to see invocation)"




    I'm assuming this has something to do with the fact that I am yet to enter any code into the "std:stream& operator << (std:stream& os, constheltal& rhs)"? However, I still don't know what to put there.. I understand that you wouldn't tell me what to write, and believe me, I want to understand this, but any hints are appreciated!

  9. #9
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    You should put in once.
    If you have so hard time overloading the << operator (which what one usually do), just print the data members of the class!
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

  10. #10
    Registered User
    Join Date
    Oct 2006
    Posts
    3,445
    Quote Originally Posted by staffank View Post
    ld: 2 duplicate symbols for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)"
    try preceding the function definition with the keyword "inline"
    that will prevent those duplicate symbol errors, particularly when the function definition is in a header file.

    Quote Originally Posted by staffank View Post
    I'm assuming this has something to do with the fact that I am yet to enter any code into the "std:: ostream& operator << (std:: ostream& os, const heltal& rhs)"?
    what the ostream << operator does is allows you to print a representation of your object to an output stream. since your heltal class only has the one data member, it might just make sense to say

    Code:
    os << rhs.getValue();
    but if you want to somehow represent it differently, other than simply a numeric output, you'd have to figure that out yourself.

  11. #11
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by Elkvis View Post
    try preceding the function definition with the keyword "inline"
    that will prevent those duplicate symbol errors, particularly when the function definition is in a header file.



    what the ostream << operator does is allows you to print a representation of your object to an output stream. since your heltal class only has the one data member, it might just make sense to say

    Code:
    os << rhs.getValue();
    but if you want to somehow represent it differently, other than simply a numeric output, you'd have to figure that out yourself.





    Again, thank you for helping me, most of the issues are now resolved!

    This is the new "heltal.h" file:



    Code:
    
    #include <iostream>
    #include <ctime>
    #include <cstdlib>
    
    
    
    
    
    
    
    
    
    
    class heltal       
    {
    private:
        int tal;       
    public:
        heltal();               
        heltal(int nmr);      
        void setValue(int value, int nmr);  
        int getValue() const;             
        heltal operator< (heltal &heltal);  
        heltal operator> (heltal &heltal);  
    
    
    };
    
    
    
    
    
    
    inline std::ostream& operator << (std::ostream& os, const heltal& rhs)
    {
        os << rhs.getValue();
    
        return os;
    }
    
    
    
    
    
    
    
    
    class Array             
    {
    private:
    heltal *ptrHeltal;      
        int size;              
    
    public:
        Array();                
        Array(int arraysize);   
        ~Array();               
    
        int generate_numbers(int arraysize, int high, int low)      
        {
            int i;          
            int x;
    
    
    
    
    
            srand((unsigned)time(0));    
    
    std::cout << "\n\nThe generated numbers are:\n";
    
            for (i = 0; i < arraysize; i++)
            {
                ptrHeltal[i] = i;
            }
    
            for (x = 0; x < arraysize; x++)
            {
    ptrHeltal[x] = (rand() % high + low);  
                ptrHeltal[x] = ptrHeltal[i];
                std::cout << "\n" << ptrHeltal[x];
            }
    
    
    
    
            return 0;
        }
    
    
    
    
    };
    
    
    






    However, now the program wont print anything but zeros, it prints the right amount of zeros though.. As you can see, I've tried to mix things up to make it work, but it won't..

    Any last addition hints? Appreciate it in that case.

  12. #12
    Registered User
    Join Date
    Jan 2013
    Posts
    5

    Code:
    for (x = 0; x < arraysize; x++) {
      ptrHeltal[x] = (rand() % high + low);
      ptrHeltal[x] = ptrHeltal[i]; // that's why you keep getting the same value
      std::cout << "\n" << ptrHeltal[x];
    }

    comment that line and you get it fixed, hopefully.

  13. #13
    Registered User
    Join Date
    Jan 2013
    Posts
    6
    Quote Originally Posted by zw_hat View Post

    Code:
    for (x = 0; x < arraysize; x++) {
      ptrHeltal[x] = (rand() % high + low);
      ptrHeltal[x] = ptrHeltal[i]; // that's why you keep getting the same value
      std::cout << "\n" << ptrHeltal[x];
    }

    comment that line and you get it fixed, hopefully.



    Thank you for your answer! I tried simply removing that line, which now makes the program print the number "16064116160" the amount of times specified by the user input in "arraysize"..

    I really can't figure out what I'm doing wrong..

    A thought:

    Could I write the code like this?


    Code:
    for (x = 0; x < arraysize; x++)
    {
    
    i = (rand() % high+low);
    ptrHeltal[i] = i;
    
    std::cout << "\n" << i;
    
    }

    Naturally, this prints the numbers, but does it also mean that the function prints the array? In the next step I'm required to make a quicksort with the array as a parameter, would that work?

  14. #14
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    This does not look right:
    Code:
    heltal::heltal(int nmr)
    {
        nmr = tal;         
    }
    
    
    void heltal::setValue(int value, int nmr)
    {
        nmr = value;            
    }
    The constructor should assign nmr to tal, and not tal to num.

    The second function does not do anything useful as it operates upon only its arguments nmr and value... and since the variables are passed by value, calling the function has no effect on the passed in parameters. You probably mean to assign something to tal but that's just a guess. There should also probably be only one argument to this function.






    Your comparison operators make no sense, unless I'm just not seeing something:
    Code:
    class heltal       
    {
        ...
        heltal operator< (heltal &heltal);  
        heltal operator> (heltal &heltal);  
    };
    
    ...
    
    heltal operator< (heltal tal, heltal value)    
    {
        tal = value;
        
        return tal;
        
    }
    
    
    heltal operator> (heltal tal, heltal value)     
    {
        value = tal;
        
        return value;
    }
    These functions should return a bool (true/false) value. The function should be const like your getValue function and they should accept a const reference to the heltal object being compared against. You should also change the variable names to be something other than the same name as the type itself. Lastly, your header and source files are saying different things about what arguments these functions should expect. If I were writing these I'd do something like this:
    Code:
    class heltal       
    {
        ...
        bool operator< (const heltal &rhs) const;
        bool operator> (const heltal &rhs) const;
    };
    
    ...
    
    bool heltal::operator< (const heltal& rhs) const
    {
        return tal < rhs.tal;
    }
    
    
    bool heltal::operator> (const heltal& rhs) const
    {
        return tal > rhs.tal;
    }





    Code:
    class Array             
    {
        ...
    
        int generate_numbers(int arraysize, int high, int low)    
        {
            ...
    There is no reason you should need to include the arraysize argument for this function. Once the Array object is created it already knows its size. This size is stored in the object's arraysize member and does not need to be passed into the function... just use it in the function's code.






    Code:
    ptrHeltal[x] = (rand() % high + low);
    First, assuming high and low are meant to be ranges for your random numbers, you are calculating this range incorrectly. A range of random values in the interval from low..high should be calculated as rand() % (high - low + 1) + low.

    Second, after the Array class constructor has run, it has already constructed its heltal members during its calling of operator new. At this point in the code, to assign this value to the heltal member you should use the heltal object's setValue member function.





    Other issues may exists beyond what I've pointed out.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. i give up
    By leahknowles in forum C Programming
    Replies: 4
    Last Post: 03-31-2011, 03:07 PM
  2. Please Give Me what i want..
    By chottachatri in forum C++ Programming
    Replies: 5
    Last Post: 03-24-2008, 04:11 AM
  3. Replies: 1
    Last Post: 06-29-2004, 05:23 PM
  4. Why to give h e l p?
    By money? in forum A Brief History of Cprogramming.com
    Replies: 35
    Last Post: 08-04-2003, 08:15 PM
  5. can someone give me a little help
    By Ckruszy in forum C++ Programming
    Replies: 3
    Last Post: 05-19-2003, 01:03 AM