Can't call class method of type string

I'm trying to call the method "toString()" in my main.cpp file, but when i do it gives me the error "C3861: 'toString': identifier not found" at line 48 of main.cpp. This is strange to me since calling any other one of the methods works like a charm. I've scoured the internet for a solution, and sat unsuccessfully for hours on end trying to solve this, but I can't find whats wrong. Am I missing something here?

players.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <string>
#include <stdio.h>

#ifndef PLAYERS_H
#define PLAYERS_H
using namespace std;

class Player
{
private:
	int numberOfMatches;
public:
	string firstName;
	string lastName;
	int birthyear;
	string* matchDates;

	string toString();
	void addMatchDate(string date);
	Player(string firstName, string lastName, int birthyear);
	Player();
	~Player();
	void save(ofstream*);
};


#endif 

players.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <stdio.h>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include "players.h"

using namespace std;

string Player::toString()
{
	int i;
	string sBirthyear = static_cast<ostringstream*>( &(ostringstream() << birthyear) )->str();

	string dates = "Name: " + firstName + " " + lastName + "\nBirthyear: " + sBirthyear + "\nMatch dates: ";
	for(i=0;i<(numberOfMatches-1);i++)
	{
		dates += (matchDates[i] + ", ");
	}
	dates += matchDates[i];
	return dates;
}

void Player::addMatchDate(string date)
{

}

Player::Player(string firstName, string lastName, int birthyear)
{
	matchDates=NULL;
	numberOfMatches = 0;
	this->firstName=firstName;
	this->lastName=lastName;
	this->birthyear=birthyear;
}

Player::Player()
{
	firstName="\0";
	lastName="\0";
	birthyear=0;
	matchDates=NULL;
	numberOfMatches = 0;
}

Player::~Player()
{
	delete[] matchDates;
}

void Player::save(ofstream*)
{

}

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <stdio.h>
#include <crtdbg.h>
#include <fstream>
#include <string>
#include <iostream>
#include "players.h"

using namespace std;

int main()
{	
	_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF|_CRTDBG_LEAK_CHECK_DF);
	ifstream dataIn;
	dataIn.open("players.txt");
	if(dataIn.is_open())
	{
		string aLine;
		
		if(!dataIn.fail())
		{
			getline(dataIn, aLine);
			int nrOfPlayers = atoi(aLine.c_str());
			Player* players;
			players = new Player[nrOfPlayers];
			for(int i=0;i<nrOfPlayers;i++)
			{
				getline(dataIn, aLine);
				players[i].firstName = aLine;
				getline(dataIn, aLine);
				players[i].lastName = aLine;
				getline(dataIn, aLine);
				int birthyear = atoi(aLine.c_str());
				players[i].birthyear = birthyear;

				getline(dataIn, aLine);
				int matchdates = atoi(aLine.c_str());
				players[i].matchDates = new string[matchdates];
				for(int j=0;j<matchdates;j++)
				{
					getline(dataIn, aLine);
					players[i].matchDates[j] = aLine;
				}
			}

		}
		
	}
	Player me;
	string tostring = me.toString();
	cout << tostring;

	getchar();
}
Last edited on
Hi @Dunder,

you have to
create an instance of
your class

(Tell me if
i am missing something)

i.e.

1
2
3
4
5
//class  //instance
Player somePlayer;

           //instance  //Method
           somePlayer.DoSomething();
You have to call the toString function on a Player object;
1
2
Player p;
cout << p.toString() << endl;
I'm trying to call the method "toString()" in my main.cpp file, but when i do it gives me the error "C3861: 'toString': identifier not found" at line 48 of main.cpp.

Presumably you meant line 12, since that is the only place you attempt to call it and line 48 consists of a single closing curly brace.

To call methods on an object, you must have an object.


This is strange to me since calling any other one of the methods works like a charm.

It shouldn't be strange to you. Calling any other method in the same way will also fail.
Whoa, thanks for the fast response guys!

Hi @Dunder,

you have to
create an instance of
your class


Ohhhh, now I understand. I'm new to c++ and classes so this is kind of confusing to me.

Presumably you meant line 12, since that is the only place you attempt to call it and line 48 consists of a single closing curly brace.


Whoops, i pasted an older version of the "main.cpp"-file, updated it. My bad!

Thanks a lot guys!
You are welcome!
Topic archived. No new replies allowed.