6 Replies - 351 Views - Last Post: 20 February 2013 - 08:23 PM Rate Topic: -----

#1 Zatrekaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-February 13

Passing Strings to functions/classes

Posted 20 February 2013 - 12:18 PM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Passing Strings to functions/classes

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6111
  • View blog
  • Posts: 23,672
  • Joined: 23-August 08

Re: Passing Strings to functions/classes

Posted 20 February 2013 - 12:24 PM

The error message in its entirety would be mighty helpful here.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4293
  • View blog
  • Posts: 13,462
  • Joined: 25-December 09

Re: Passing Strings to functions/classes

Posted 20 February 2013 - 12:27 PM

Quote

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:


What is the exact error message?

What include files did you include in your header file?

Does your include file contain include guards?


Jim
Was This Post Helpful? 0
  • +
  • -

#4 Zatrekaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-February 13

Re: Passing Strings to functions/classes

Posted 20 February 2013 - 05:12 PM

There are quite a few errors since the program still has quite a few sections to be completed. However this is everything I see for anything animal related:

1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(7): error C2146: syntax error : missing ';' before identifier 'species'
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(11): error C2061: syntax error : identifier 'string'
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\plant.h(6): error C2146: syntax error : missing ';' before identifier 'planttype'
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\plant.h(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\plant.h(6): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\plant.h(10): error C2460: 'Plant::string' : uses 'Plant', which is being defined
1> f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\plant.h(2) : see declaration of 'Plant'
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\main.cpp(69): error C2661: 'Animal::Animal' : no overloaded function takes 4 arguments
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\main.cpp(91): error C2039: 'species' : is not a member of 'Animal'
1> f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(2) : see declaration of 'Animal'
1> Animal.cpp
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(7): error C2146: syntax error : missing ';' before identifier 'species'
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(7): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(11): error C2061: syntax error : identifier 'string'
1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.cpp(15): error C2511: 'Animal::Animal(int,int,float,std::string)' : overloaded member function not found in 'Animal'
1> f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(2) : see declaration of 'Animal'


Someone mentioned that in order to pass a string, I must use pointers and pass by ref instead of by value. Any insight on that?
Was This Post Helpful? 0
  • +
  • -

#5 Zatrekaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-February 13

Re: Passing Strings to functions/classes

Posted 20 February 2013 - 06:04 PM

UPDATE:

I edited the code, and have it down to one error message...

1>f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.cpp(15): error C2511: 'Animal::Animal(int,int,float,std::string)' : overloaded member function not found in 'Animal'
1> f:\dropbox\c++\csc317 programs\jm_opp_assignment2\jm_opp_assignment2\animal.h(2) : see declaration of 'Animal'

current code:

main.cpp:
void fileread()
{
	string id;
	std::string species;

	int age, weight;
	float energy;

	ifstream data;

	//Opens input file, located in root
	data.open ("input.txt", ios::in);									

	if (data.is_open())
	{
		//Records data entry type
		data >> id;											
		while (!data.eof())
		{

			if (id == "Animal")
			{
				//Making the Animal1 object
				Animal Animal1;		

				//Data reading & manipulation
				data >> age;
				age = age * 365;
				data >> energy;
				data >> weight;
				data >> species;

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


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

Animal::Animal()
{
}


Animal::~Animal()
{
}

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


This seems to be where the error is. the second "Animal" has a red line under it, and says "No instance of Overloaded function "Animal::Animal" matches the specified type.

This post has been edited by Zatrekaz: 20 February 2013 - 06:07 PM

Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1112
  • View blog
  • Posts: 4,619
  • Joined: 09-June 09

Re: Passing Strings to functions/classes

Posted 20 February 2013 - 08:08 PM

Quote

This seems to be where the error is. the second "Animal" has a red line under it, and says "No instance of Overloaded function "Animal::Animal" matches the specified type.

Can you post your class definition?
Was This Post Helpful? 0
  • +
  • -

#7 Zatrekaz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 22
  • Joined: 04-February 13

Re: Passing Strings to functions/classes

Posted 20 February 2013 - 08:23 PM

I have actually gotten it to work... surprised even myself.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1