Hey guys, i am working on a project which basically takes a string which is being inputted by people and then adding it together and displaying in a certain format. What i am having problem is when i try to read the string and store it into the functions.

class Quarternoin
{
   friend ostream& operator<<(ostream& , const Quarternoin&);
   friend istream& operator>>(istream& ,Quarternoin& ) ;
public:
void Quarternoin (string) ;

 void showSale();
private :
    int value1a , value1b , value1c , value1d ;
    int value2a , value2b , value2c , value3d;
    string LineA;
    string LineB;
};

ostream& operator<<(ostream &out , const Quarternoin &total ){
    out << total.value1a << endl;
    return out ;
}

istream& operator>>(istream &in , Quarternoin &total ){
    cout << endl;
    in >> total.LineA ;
  
    return in;

}

void Quarternoin::Quarternoin(string c)
{ 
 value1a = atoi (c.c_str());

 }

int main()
{
  Quarternoin A;

  cout << "enter something : " ;
  cin >> A ;

  cout << A << endl;
  return 0 ;
}

The problem i am facing is it seems whatever value i input into the program, it is not stored into A. Can sombody point out how am i able to fix this? Really hope for some help. This is the only part of the program left.

Recommended Answers

All 13 Replies

void Quarternoin::Quarternoin(string c){  value1a = atoi (c.c_str());  }

This is your problem. The constructor should be defined without a type preceding it.Like this

Quarternoin::Quarternoin(string c){  value1a = atoi (c.c_str());  }

Ohh :) and try to initialize it too .In function main:

string str;
cin >> str;
A(Str);

Well , i \placed the void there cos i was having some error mistakes. When i removed the void like u told me to : i got errors saying...

line 37: error: no matching function for call to`Quarternoin::Quarternoin()'

line 2: note: candidates are: Quarternoin::Quarternoin(const Quarternoin&)


 line 6 :note:            Quarternoin::Quarternoin(std::string)

this are the errors which appear when i take out void.

As for your second solution, i got to do it by using the template main which my lecturer gave us. Thus that is why my main is what it is...

Sorry i didn't see the declaration.Thought it was a constructor.Anyway you don't have a value there so you should try providing a constructor or use

A.Quarternoin(str);

As for what you are saying about templates .. you don't have a template class and even if you did you cannot create an instance of a template object without a specialization.What are you trying to do anyway?

you mean using it in the main? i cant use it in the main , due to the restrictions my lecturer gave me. I got to use the

Quaterion A ;
cout << Enter your input : "
cin << A;

What i am trying to do is to take a string value and convert it into an integer that all. but my main problem is i cant seem to get the value into the variable.

Both lines 6 and 29 should definitely not have the void type, constructors do not have types.

The reason your output stream handler doesn't show the input is that the input stream handler writes to LineA but the output stream handler outputs valuea. Nothing in the called code converts what is read into LineA into a value in valuea.

#include <iostream>

using namespace std;

template <typename C> struct Q
  {
    C thing;
    int convert() {return atoi(thing.c_str()); }
    friend ostream& operator << (ostream& o,Q t) {return o << t.convert();}    
    friend istream& operator >> (istream& i,Q& a) {return i >> a.thing;}
  };

int main ()
  {
    Q<string> a;
    cin >> a;    
    cout << a;
  }

It sure's ulgy.Who would want that ?

Could you give an example of how the coding should be like then?

I think there's some confusion in terminology is all. I believe when the OP said "template main" he literally meant code his instructor had given him to use as a model to follow (not in the sense of generic programming).

Did you get this resolved, OP, after that detour? The key really is making a default constructor (one with no arguments) as that is what is called when you declare Quaterion A; .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.