Using cin and cout to populate fields of a class C++
The compiler fails to see the operator overloads in your main.cpp
translation unit, because the overloads are not found in that file, nor are they found in any of the #include
files. You need to declare both overloads in your MyClass.h
file instead, after the MyClass
declaration:
MyClass.h:
#ifndef MyClass_h
#define MyClass_h
#include <iostream>
#include <stdio.h>
#include <string>
using namespace std;
class MyClass {
public:
string input
void ReadFrom(istream &is);
void WriteTo(ostream &os) const;
};
istream& operator >>(istream &is, MyClass &cls);
ostream& operator <<(ostream &os, const MyClass &cls);
#endif /* MyClass_h */
You can leave the definitions as-is in your MyClass.cpp
file.
How to your own C++ cout like object
I'm not sure if I interpretted your question correctly but I think you want a class with an overloaded operator<< so that's what i have here
class MyClass {
public:
MyClass() = default;
MyClass& operator<<(int input) {
//do something with input
return *this;
}
}
You would use it like this;
MyClass myObject;
myObject << 42;
//the function would have been called
cout a function of a class
You did not define the operator <<
for your class Complex
. So the compiler does not know how to output an object of the class.
You could define it for example the following way
std::ostream & operator <<( std::ostream &os, const Complex &c )
{
return os << "{ " << c.GetRe() << ", " << c.GetIm() << "i }";
}
To do this the member functions GetRe
and GetIm
shall be declared with the qualifier const
. For example
float GetRe() const
{
return Re;
}
float GetIm() const
{
return Im;
}
After that you can write
std::cout << z1.add(z2) << '\n';
Pay attention to that the member function add
also should be declared like
Complex add( const Complex &s2 ) const
{
Complex sum(Re + s2.Re, Im + s2.Im);
return sum;
}
Or just
Complex add( const Complex &s2 ) const
{
return { Re + s2.Re, Im + s2.Im };
}
Passing data types to C++ class contructor
Not commenting on whether you could use vector
instead of int *my_dynamic_array
, I just translated your code into a template. Hope it helps somehow:
template<typename T>
class ImageClass {
public:
ImageClass<T>() {
cout << "Please enter the size of array: ";
cin >> this->size;
my_dynamic_array = new T[this->size];
SetPrivate();
}
void SetPrivate() {
cout << "\n\nPlease enter " << this->size << " numbers for the array elements...";
for (int i = 0; i < this->size; i++) {
cout << "\n\n#" <<i+1<<" : ";
cin >> my_dynamic_array[i];
}
}
protected:
int size;
T *my_dynamic_array;
};
int main() {
ImageClass<int> ic_i;
ImageClass<double> ic_d;
}
C++ : &(std::cout) as template argument
This fixes your code, omit the parenthesis:
#include <iostream>
template<std::ostream* stream>
class MyClass
{
public:
void disp(void) {
(*stream) << "hello";
}
};
int main(void)
{
MyClass<&std::cout> MyObj;
MyObj.disp();
return 0;
}
Live Demo
A more detailed explanation why can be found here:
Error with address of parenthesized member function
Related Topics
Significance of Ios_Base::Sync_With_Stdio(False); Cin.Tie(Null);
Does the Size of an Int Depend on the Compiler And/Or Processor
Generate Random Numbers Using C++11 Random Library
Officially, What Is Typename For
Conveniently Declaring Compile-Time Strings in C++
Why Does Subtracting '0' in C Result in the Number That the Char Is Representing
How to Use Boost in Visual Studio 2010
Inline Functions VS Preprocessor Macros
What Happens to a Detached Thread When Main() Exits
Namespace + Functions Versus Static Methods on a Class