Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Passing Strings to functions/classes

$
0
0
I have the need to pass a string to a class. Am I missing something, as the .cpp file of the class throws an error on the type with this code:

Inside Main.cpp I have the function call inside a file read... this is a continuation from a previous program... here is the code snippet that would be useful:

main.cpp:
	if (id == "Animal")
			{
				//Making the Animal1 object
				Animal Animal1;		

				//Data reading & manipulation
				data >> age;
				age = age * 365;
				data >> energy;
				data >> weight;
				data >> species; //*****string that needs to be passed to the function, pulls from input.txt file*****

				//Inserts pointer to object to the back of this list
				animalList.push_back(new Animal(age,weight,energy,species));	
			}



Animal.h:
class Animal
{
public:

	int age, weight;
	float energy;
	string species;

	Animal();
	~Animal();
	Animal(int, int, float, string);
};



Animal.cpp:
#include "Animal.h"
#include <iostream>
using namespace std;

Animal::Animal()
{
}


Animal::~Animal()
{
}

Animal::Animal(int a, int w, float e, string s)
{
	age = a;
	weight = w;
	energy = e;
	species = s
	
}



Again, I need to pass the string species into the Animal class.

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>