Hey all!

I am trying to build this code and I am running into a bit of trouble. Visual c++ gives me this error:

>Profile.cpp
1>c:\users\janice\documents\visual studio 2008\projects\assignment6\assignment6\profile.cpp(101) : error C2027: use of undefined type 'Post'
1> c:\users\janice\documents\visual studio 2008\projects\assignment6\assignment6\profile.h(6) : see declaration of 'Post'
1>c:\users\janice\documents\visual studio 2008\projects\assignment6\assignment6\profile.cpp(101) : error C3861: 'setPost': identifier not found
1>c:\users\janice\documents\visual studio 2008\projects\assignment6\assignment6\profile.cpp(102) : error C2027: use of undefined type 'Post'
1> c:\users\janice\documents\visual studio 2008\projects\assignment6\assignment6\profile.h(6) : see declaration of 'Post'
1>c:\users\janice\documents\visual studio 2008\projects\assignment6\assignment6\profile.cpp(102) : error C3861: 'getPost': identifier not found
1>Build log was saved at "file://c:\Users\Janice\Documents\Visual Studio 2008\Projects\Assignment6\Assignment6\Debug\BuildLog.htm"


So, basically, the addToWall function is in the Profile class (not Post class) and is calling a function in the Post class ... and it is returning an error.

Please help! Thank you so much!

//Post Class Definition
//The Post class will serve as a Outbox or a Sent messages array
//That means that each person has their own Outbox 

#include <string>
#include <iostream>

using namespace std;

const int maxNumPosts = 500;

class Profile; 

class Post {
public:
	// Constructor that sets name an city and put this profile in allProfiles
	Post(string inputName); 
	void setPost(Profile* person, char); // Creates the Outbox array
	char * getPost(); // Will return a particular post to build someone's wall


private:
	void setName(string);
	string getName();
	string name;
	static int numPosts; // Number of posts each person made so far
	static char posts[maxNumPosts]; //So each person has an outbox of posts




};
// This is in the Post class
void Profile::addToWall(Profile *person, string inputText) // will take the persons outbox and add link it to a wall
{
	cin >> inputText; // input the text
	Post::setPost(this, inputText); // send the current object and the text to setPost function
	theWall[wallPastes] = Post::getPost(); // now, get the post back and assemble the wall
	wallPastes++;
}

Recommended Answers

All 3 Replies

> use of undefined type 'Post'
It means you're trying to use stuff from the Post class before you've fully defined the class itself. It's hard to say without seeing all of your code and how it's structured, but Ed would guess that you have a circular dependency and are confused about how to resolve it.

> Post::setPost(this, inputText);
> theWall[wallPastes] = Post::getPost();
This is suspicious code. I want to say that it shouldn't compile, but Edward hasn't seen all of the code yet and can't be sure.

Thank you Edward for the help! I understand there probably is circular dependency - in fact im pretty sure half my pointers dont work properly.... In any case, here is the rest of the code:

//This is Post.cpp
#include <string>
#include <iostream>
using namespace std;

#include "Post.h"

//Initialize Static Things
int Post::numPosts = 0;
char Post::posts[] = {};

Post::Post(string inputName) 
{
	 setName(inputName);
}
void Post::setName(string inputName)
{
	name = inputName;
}

string Post::getName()
{
	return name;
}

void Post::setPost(Profile *person, char actualpost)
{ 
	posts[numPosts] = actualpost;
	numPosts++;
}

char * Post::getPost() 
{ 
	return &posts[numPosts];
}
// Profile.h : Header file for Profile class

#include <string>
#include <iostream>

class Post;

using namespace std;

const int maxProfiles = 500; // Maximum number of total profile
const int maxWallPastes = 500; // Maximum number of wall pastes

class Profile {
public:
	// Constructor that sets name an city and put this profile in allProfiles
	Profile(string inputName, string inputCity); 
	
	// Note: friend is a reserved word, don't use it as a variable name.

	void displayFriends(); // Display all of the Profile's friends
	void addFriend(Profile* person); // Add a friend.
	int isFriend(Profile* person); // Is person a friend?
	bool isFriendOfFriend(Profile* person); // Is person a friend of a friend?
	void displayLocalProfiles(); // Display names of all profiles in your city.
	void addToWall(Profile* person, string inputText); 


private:
	
	// Private functions
	void setNumFriends(int inputNumFriends); // set numFriends
	int getNumFriends(); // return numFriends
	Profile* getFriend(int i); // return friends[i]
	void setFriend(int i, Profile* inputFriend); // set friends[i]
	void setCity(string inputCity); // set City
	string getCity(); // get City
	string getName(); // return name
	void setName(string inputName); // set name
	char displayWall(); // Displays the person's wall
	
	// Private Data
	static Profile* allProfiles[maxProfiles]; // Array of pointers to all profiles
	static int numProfiles; // Number of profiles defined so far
	static Post* theWall[maxWallPastes]; //Array of pointers to Outbox posts
	static int wallPastes;
	string city; // Profile City
	int numFriends; // Number of friends
	Profile* friends[maxProfiles]; // array of pointers to friends
	string name; // Name of person for this profile
};
#include <string>
#include <iostream>
using namespace std;

#include "Profile.h"

// initialize static class members
int Profile::numProfiles = 0;
Profile* Profile::allProfiles[] = {};
int Profile::wallPastes = 0;
Post* Profile::theWall [] = {};

Profile::Profile(string inputName, string inputCity) {
	setName(inputName); // Store Name
	setCity(inputCity); // Store City
	setNumFriends(0); // initialize numFriends
	for(int i = 0; i < maxProfiles; i++){
		friends[i] = NULL; // initialize friends array
	}
	// add a pointer to the constructed Profile to the allProfiles array
	allProfiles[numProfiles] = this;
	numProfiles++;
}

string Profile::getName(){ // return name
	return name;
}

void Profile::setName(string inputName){ // set name
	name = inputName;
}

string Profile::getCity(){ // return city
	return city;
}

void Profile::setCity(string inputCity){ // set city
	city = inputCity;
}

int Profile::getNumFriends(){ // return numFriends
	return numFriends;
}

void Profile::setNumFriends(int inputNumFriends){ // set numFriends
	numFriends = inputNumFriends;
}

Profile* Profile::getFriend(int i){ // return friends[i]
	return friends[i];
}

void Profile::setFriend(int i, Profile* inputFriend){ // set friends[i]
	friends[i] = inputFriend;
}

void Profile::displayFriends(){
	cout << getName() << " has the following (" << getNumFriends() << ") friends:" << endl;
	for(int i = 0; i < getNumFriends(); i++){ // numFriends tells us how far to iterate
		cout << "  " << getFriend(i)->getName() << endl; // each friend's name is output
	}
}

void Profile::addFriend(Profile* person){// Add a friend.
	setFriend(getNumFriends(),person); // store the input pointer in the friends array
	setNumFriends(getNumFriends()+1); //get the numFriends variable incremented to keep up with the new friends array
}

int Profile::isFriend(Profile* person){ // Is person a friend?
	for(int i = 0; i < getNumFriends(); i++){
		if(getFriend(i) == person){ // if the input pointer matches a pointer in the friends array
			return i;
		}
	}
	return 0; // if we get here, the input doesn't match any of our friends
}

bool Profile::isFriendOfFriend(Profile* person){ // Is person a friend of a friend?
	for(int i = 0; i < getNumFriends(); i++){
		if(getFriend(i)->isFriend(person)){ // each friend calls isFriend
			return true;
		}
	}
	return false; // if we get here, no friend has the input as a friend
}

void Profile::displayLocalProfiles(){
	cout << getName() << " has the following local Profiles:" << endl;
	for(int i = 0; i < numProfiles; i++){
		Profile* tempProfilePtr = allProfiles[i]; // foreach element in allProfiles
		if((tempProfilePtr != this) && // if it's not the calling Profile
		   (tempProfilePtr->getCity() == getCity())){ // and has the same city as the calling profile
			cout << "  " << tempProfilePtr->getName() << endl; // output!
		}
	}
}

void Profile::addToWall(Profile *person, string inputText) // will take the persons outbox and add link it to a wall
{
	cin >> inputText; // input the text
	Post::setPost(this, inputText); // send the current object and the text to setPost function
	theWall[wallPastes] = Post::getPost(); // now, get the post back and assemble the wall
	wallPastes++;
}

char Profile::displayWall()
{
	for(int i = 0; i < wallPastes; i++)
	{ 
		cout << "  " << theWall[i] << endl; // each friend's name is output
	}
}

Sorry about posting all that, I was trying to avoid posting so many lines of code =)

Anyway, there are 4 code blocks (one is in the last post)

bascially, im trying to call a function (defined in the Post class) to do some work for me in the Profile class

Thank you again!

I put the four files into Visual C++ and tried to compile. Regarding this error:

Profile.cpp
1>c:\users\janice\documents\visual studio 2008\projects\assignment6\assignment6\profile.cpp(101) : error C2027: use of undefined type 'Post'

If you look at your Profile.h code,

// Profile.h : Header file for Profile class
#include <string>
#include <iostream>

class Post;

using namespace std;

const int maxProfiles = 500; // Maximum number of total profile
const int maxWallPastes = 500; // Maximum number of wall pastes

class Profile {

You have line 5 telling the compiler "be on the lookout for the Post class", but you don't have this line:

#include "Post.h"

If you put that at the top, that error goes away (though new ones will pop up). Add that line at the top of your file. If you get any kind of multiple definition errors as a result, you can add these lines to the top of Profile.h:

#ifndef PROFILE_H
#define PROFILE_H

and these lines to the top of Post.h

#ifndef POST_H
#define POST_H

and this line at the bottom of both Post.h and Profile.h

#endif
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.