Stuck on an annoying issue

Hello there!

This is my first post on this forum ever.
I have for the past 2 weeks been working on this issue and I can not for the life of me figure it out. After having 65 million tabs open searching for the answer in my browser, I finally thought I'd post my issue myself.

Anyway, I'll post the code I've got, the program should be done but I can't get it to print what it should...


the .h file is as follows:



#include <iostream>
#include <ctime>
#include <cstdlib>
using namespace std;




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 random numbers are:\n";

for (i = 0; i < arraysize; i++)
{
ptrHeltal[i] = (rand() % high + low);
}

for (i = 0; i < arraysize; i++)
{
cout << "\n" << ptrHeltal[i];
}


return 0;

}

};





I have two .cpp files besides the main.cpp

One is called "heltal.cpp" and is as follows:

#include <iostream>
#include "heltal.h"
using namespace std;


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;
}


heltal operator< (heltal tal, heltal value)
{
tal = value;

return tal;

}

heltal operator> (heltal tal, heltal value)
{
value = tal;

return value;
}




The other is called array.cpp and is as follows:


#include <iostream>
#include <ctime>
#include <cstdlib>
#include "heltal.h"


using namespace std;



Array::Array()
{
size=50;
ptrHeltal = new heltal[size];

}

Array::Array(int arraystorlek)
{

size = arraystorlek;
ptrHeltal = new heltal[size];


}




Array::~Array()
{

delete []ptrHeltal;

}




The main.cpp is as follows:


#include <iostream>
#include "heltal.h"
using namespace std;




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;
}




Now, the issue is in "heltal.h", the function generate_numbers in the class Array. When I try to print the array, I get an error saying "Invalid operands to binary expression ('basic_ostream<char_std::__1::char_traits<char>>' and 'heltal').

At one point Xcode asked my to put a & before the ptrHeltal[i] in the cout, and that printed the correct number of "things" (arraysize), but just printed something like 0x573921c0...



Any tips or suggestions would be deeeeeply appreciated, and I'm sorry for the long post!


Thank you for your time.


1
2
3
4
for (i = 0; i < arraysize; i++)
{
cout << "\n" << ptrHeltal[i];
}

You didn't implement operator << for Heltal.
It shoud be something like
std::ostream operator<<(std::ostream out, heltal in)
Last edited on
Thank you for your answer!

This did remove the litte red dot that showed up when I try to print the array, however, it now says "Expression result unused" after ptrHeltal[i]; and also, I can't use cout anymore, and I can't figure out how to print it now..

Helpful for any additional help!
Topic archived. No new replies allowed.