Sign in to follow this  

So many errors

This topic is 3596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I decided to move from java to c++ this weekend from java. I read for about three hours and then sat down and tried to code. I typed for a while and then compiled. It came up with about 50 errors. I think that it isn't allowing me to put my user defined classes as return types. *moan* I am so confused. Could someone please point me out on what I'm doing wrong? and what to improve in order for it to at least compile. i'm using Microsoft's Visual C++ express edition. Sorry if I'm asking to much of you guys.
//PersonList.h
#ifndef PersonList_H
#define PersonList_H

class PersonList {
	
private:
	Person *personList[20];
	Person *ptrToPerson;
public:
	PersonList(Person *[]);
	Person []getPersonList();
	PersonList();
	~PersonList();
}

class Person {
private:
	char *lastName;
	char *firstName;
public:
	Person(char * , char*);
	Person();
	~Person();
	char *getFirstName();
	char *getLastName();
	
}
#endif

char *Person::getFirstName(){
	return firstName;
}

char *Person::getLastName(){
	return lastName;
}

Person::Person(){}

Person::~Person(){}

Person::Person(char *firstname , char *lastname){
	firstName = firstname;
	lastName = lastname;
}

PersonList::PersonList(){}

PersonList::PersonList(Person *listOfPeopleToAdd[]){
	for(int i = 0 ; i < 20 ; i++){
		personList[i]->firstName = &(listOfPeopleToAdd[i]->firstName);
		personList[i]->lastName = &(listOfPeopleToAdd[i]->lastName);
	}
}



thanks.

Share this post


Link to post
Share on other sites
You need to define the Person class before PersonList can use Person. In other words, as the compiler is reading your code from top to bottom, left to right, it will first come across PersonList. However the compiler can't continue because it uses Person which is not yet defined. Try moving the PersonList class declaration under Person.

Share this post


Link to post
Share on other sites
Any particular reason that you're migrating from Java to C++? Depending on what your goals are, you might do better to stick with Java, or to try, say, Python.

In any case, at least some of the errors you're getting are probably related to your attempt to return an array from a function. In C++, arrays are not first-class types; you can't pass them to functions by value, and you can't return them from functions as you're attempting to do in your code.

To work around this, it's common in C++ to use a class or struct to represent an array; you then have an 'actual object' to work with, which can be passed around freely.

The C++ standard library provides a class template - std::vector - that represents a dynamically resizable array. For fixed-size arrays, you can use boost::array. For n-dimensional arrays, there's boost::multi_array.

Note that the Boost libraries are not part of the SC++L - you have to download them separately. (Some parts of the Boost libraries are however being incorporated into the next version of the C++ standard.)

Also, although the way you're handling the 'first name' and 'last name' strings may appear to work (at least initially), it will most likely blow up in your face sooner or later. I'm not going to try to cover pointers and constant correctness in this post, but in short: use std::string instead.

Share this post


Link to post
Share on other sites
Unlike Java, which has independent compilation units, C++ files are parsed top to bottom. Whatever you want to use needs to be declared before you use it.


Remember Java collections? C++ has them. Use them.

#include <vector> // java.util.list

std::vector< Person > personList; // replaces your PersonList



char * is bad. Java has string class. So has C++.


#include < string >

class Person {
private:
std::string lastName;
std::string firstName;
public:
Person(std::string, std::string );
Person();
~Person();
std::string getFirstName();
std::string getLastName();

}


C++ collections have nifty tools you can use to manage collections safely.


PersonList::PersonList(std::vector<Person> listOfPeopleToAdd)
{
std::vector<Person>::iterator i = listOfPeopleToAdd.begin();
for ( ; i!=listOfPeopleToAdd.end(); ++i) {
personList.push_back(*i);
}
}


And this is just the start.

I won't go into details of why your original code has more memory leaks than a sieve.

But that's where the fun beings. Then there's object ownership, references, pointers, passing by value, .....

Share this post


Link to post
Share on other sites

This topic is 3596 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this