I have tried to create a simple class where the program passes a name and telephone number to my class type and then prints it to the screen. It tells me I have undeclared identifiers, but I am constructing my program just like an example we did in class. Can someone tell me what I am doing wrong?!

// Specification file (Time.h)
// This file gives the specification
//of a phone abstract data type

#include <iostream>
#include <string>

class Phone
{
public:
	void Set(string firstName, string lastName, int number);

	void Write() const;

	Phone(string initfName, string initlName, int initNum);

	Phone();

private:
	string fName;
	string lName;
	int Num;

};

//Implementation file (phone.cpp)

#include "Phone.h"
#include <iostream>
#include <string>

using namespace std;

void Phone::Set(string firstName, string lastName, int number)
{
	fName = firstName;
	lName = lastName;
	Num = number;
}

void Phone::Write() const
{
	cout << fName;
	cout << lName << endl;
	cout << Num;
}

Phone::Phone(string initfName, string initlName, int initNum)
{
	fName = initfName;
	lName = initlName;
	Num = initNum;
}

Phone::Phone()
{
	fName = "";
	lName = "";
	Num = 0;
}


#include "Phone.h"
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int main()
{
	Phone thePhone(Andrew, Daniel, 4906996);
	thePhone.write();
	cout << endl << endl;
	system("pause");
	return 0;
}

Edited 6 Years Ago by adaniel058: n/a

Inside the header file you have to tell the compiler that string is in std namespace. Putting using namespace std; in the *.cpp files is not sufficient because that line is located after the header file that contains the Phone class.

class Phone
{
public:
	void Set(std::string firstName, std::string lastName, int number);

	void Write() const;

	Phone(std::string initfName, std::string initlName, int initNum);

	Phone();

private:
	std::string fName;
	std::string lName;
	int Num;

};

Edited 6 Years Ago by Ancient Dragon: n/a

Thanks so much for your help! Doing that I can almost get it to work!
Now when I try to pass arguments to the functions in my class it tells me my arguments are undeclared identifiers. I assume this means I have an error somewhere in my declarations but I cant seem to figure it out. This is my first program using classes so any help would be great!

I feel like Im making this harder than it is or should be....

// Specification file (Phone.h)
// This file gives the specification
//of a phone abstract data type


#include <string>

class Phone
{
public:
	void Set(std::string firstName, std::string lastName, int number);

	void Write() const;

	Phone(std::string initfName, std::string initlName, int initNum);

	Phone();

private:
	std::string fName;
	std::string lName;
	int Num;
};

//Implementation file (phone.cpp)

#include "Phone.h"
#include <iostream>
#include <string>

using namespace std;

void Phone::Set(string firstName, string lastName, int number)
{
	fName = firstName;
	lName = lastName;
	Num = number;
}

void Phone::Write()
{
	cout << fName;
	cout << lName << endl;
	cout << Num;
}

Phone::Phone(string initfName, string initlName, int initNum)
{
	fName = initfName;
	lName = initlName;
	Num = initNum;
}

Phone::Phone()
{
	fName = "";
	lName = "";
	Num = 0;
}

// Program file

#include "Phone.h"
#include <iostream>
#include <cctype>
#include <string>

using namespace std;

int main()
{
	Phone thePhone(Andrew, Daniel, 4906996);
	thePhone.Write();
	cout << endl << endl;
	system("pause");
	return 0;
}

Edited 6 Years Ago by adaniel058: n/a

line 72: Andrew and Daniel are assumed to be variable names because they are not enclosed in double quotes.

Edited 6 Years Ago by Ancient Dragon: n/a

Generally, I wouldn't be this blatant, but I think it warranted this time.

Look at Line 72, the first line of your main().

Phone thePhone(Andrew, Daniel, 4906996);

You are trying to send Andrew and Daniel to the function. The problem is that C++ sees them as undeclared variable names. It appears that what you actually want are string literals. To create a string literal, place the information in "double-quotes".

Phone thePhone("Andrew", "Daniel", 4906996);

In this example, "Andrew" and "Daniel" are now string literals, not variable names.

EDIT:
Oops, where'd AD's reply come from???? I reloaded before I posted to be sure...

Edited 6 Years Ago by Fbody: n/a

Well I fell like a moron. I got so wrapped up in thinking it had to be something hard I never even realized I was not passing my arguments properly. Thank you both so much

One more quick question. Im getting syntax errors now that Ive created an enum data type in my header file. Any thoughts?

1>c:\program files\microsoft visual studio 9.0\vc\include\cctype(19) : error C2143: syntax error : missing ';' before 'namespace'


1>c:\users\andrew\documents\visual studio 2008\projects\phonebook\phonebook\phone.cpp(6) : error C2143: syntax error : missing ';' before 'using'

Appears to be in my .cpp file.

// Specification file (Phone.h)
// This file gives the specification
// of a phone abstract data type
// pg 551

#include <string>

enum phoneType {Home, Office, Fax, Cell, Pager};

class Phone
{
public:
	void Set(std::string firstName, std::string lastName, int number, ::phoneType newType);

	void Write() const;

	Phone(std::string initfName, std::string initlName, int initNum, ::phoneType initType);

	Phone();

private:
	std::string fName;
	std::string lName;
	int Num;
	::phoneType type;
}

//Implementation file (phone.cpp)

#include "Phone.h"
#include <string>

using namespace std;

void Phone::Set(string firstName, string lastName, int number, phoneType newType)
{
	fName = firstName;
	lName = lastName;
	Num = number;
	type = newType;
}

void Phone::Write() const
{
	cout << fName << " ";
	cout << lName << endl;
	cout << Num;
}

Phone::Phone(string initfName, string initlName, int initNum, phoneType initType)
{
	fName = initfName;
	lName = initlName;
	Num = initNum;
	type = initType;
}

Phone::Phone()
{
	fName = "";
	lName = "";
	Num = 0;
	type = Home;
}

This is your problem:

class Phone
{
public:
	void Set(std::string firstName, std::string lastName, int number, ::phoneType newType);
 
	void Write() const;
 
	Phone(std::string initfName, std::string initlName, int initNum, ::phoneType initType);
 
	Phone();
 
private:
	std::string fName;
	std::string lName;
	int Num;
	::phoneType type;
} //<----there is something missing here.....

Edited 6 Years Ago by Fbody: n/a

Thankfully I figured that out without too much trial and tribulation last night. I get so frustrated I miss the little things. Thank you Fbody

This article has been dead for over six months. Start a new discussion instead.