No match for operator cin>>

Hi i am currently doing a program for my assignment at university level. I have met with the following problems. The program takes in values from a main class(main.cpp) then transfers the values via a get method from the locationData class(location.cpp). The problem is i declared locationData as locationData data in main.cpp and tried to get a value using a getmethod which resides in the locationData class. However, when i tried to compile it it gives me an error that says in function int main(), no match for operator cin>>data.locationData::getSunType

attached is a snippet of my code

main.cpp
#include <iostream>
#include<string>
#include "locationData.cpp"


using namespace std;



void showProgram()
{
cout<<"Welcome to Mission Plan program!"<<endl<<

"1)Input statistical data."<<endl<<
"2)Compute civ. index value (for all records)"<<endl<<
"3)Print top 5 exploration destinations."<<endl<<
"4) Print total travel distance."<<endl;



}

int main()
{
locationData data;
string sunType;
int xCord,yCord,noOfEarthPlanet,noOfEarthMoon;
int choice;
float aveParticulateDensity;
float avePlasmaDensity;
showProgram();
cout<<"Please select your choice :";
cin>>choice;


switch(choice)
{
case 1 : cout<<"Input statistical data"<<endl;


cout<<"Please enter x-cordinate:";
cin>>xCord;
cout<<"Please enter y-cordinate:";
cin>>yCord;
cout<<"Please enter sun type: TYPE:";
cin>>data.getSunType(sunType);
cout<<"Please enter no. of earth-like planets:";
cin>>noOfEarthPlanet;
cout<<"Please enter no. of earth-like moons:";
cin>>noOfEarthMoon;
cout<<"Please enter ave. particulate density (%-tage):";
cin>>aveParticulateDensity;
cout<<"Please enter ave. plasma density (%-tage):";
cin>>avePlasmaDensity;

cout<<"Record succesfully stored. Going back to main menu."<<endl;
break;

default: cout<<"Invalid choice, please re-enter your choice."<<endl;

}


return 0;

}



locationData.cpp

#include<iostream>
#include<string>

using namespace std;

class locationData
{
private:
string sunType;
int noOfEarthLikePlanets;
int noOfEarthLikeMoons;
float aveParticulateDensity;
float avePlasmaDensity;

public:
void getSunType(string);
void getNoOfEarthLikePlanets(int);
void getNoOfEarthLikeMoon(int);
void getAveParticulateDensity(float);
void getAvePlasmaDensity(float);
void setSunType();

};

void locationData::getSunType(string sun)
{
sunType=sun;

}


may i know what`s wrong with the code? cos i tried every way to debug it
Last edited on
Yeah, you can't really do what you're trying to do there.

You're essentially saying "make the value from the input buffer the return value from the getSunType function)".

I think what you're trying to do, is take the string input and pass it into the function. If so, it needs to be done like this:
1
2
3
cout << "Please enter sun type: ";
cin >> sunType;
data.getSunType(sunType);
Hi hutch i tried that too. But it returns another error.

tmp/ccLawY4W.o: In function `locationData::getSunType(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
locationData.cpp:(.text+0x0): multiple definition of `locationData::getSunType(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'


any idea what does this mean?
You shouldn't include .cpp files. Now you are including the definition of getSunType in multiple files so the function is being defined multiple times so that is probably why you get the error. Instead you should create a header file (locationData.h) that you include (#include "locationData.h"). The header file should contain the class definition, while the .cpp file contains all the function definitions of the class.

In case you are not familiar with header files I better mention that you should also use include guards. http://en.wikipedia.org/wiki/Include_guard
Hi Peter thanks for your help. I am rather new at C++. Okay sure thanks, i will read up on header files and use guards for now.
Topic archived. No new replies allowed.