String as parms or function types?

I'm hoping this is an easy question, so I'm not going to post code right now. If it turns out that it's more complicated than I think, I can certainly post code segments.

I've built a class (of which I will declare an array), and I'd like two of the data elements to be strings. Ideally, I'd also like to create functions that either return strings, or that use strings as part of the parameter list. So far though, I've gotten compiler errors when I've tried that.

One option, and the easiest answer (though the one I don't want) is that it just can't be done. Hopefully, that's not the answer.

Thanks
Hopefully, I've pulled the right segments, but here are some pieces of the code I am working on.

From the Driver
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>

#include "team.h"	//20100720

string ncity, nname;

using namespace std;

int main{ ... }

void CreateTeam()
{
	cout << "Please enter the name of the city.\n";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	getline (cin, ncity);
	cout << "Please enter the name of the team.\n";
	cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
	getline (cin, nname);
	Team::NewTeam(ncity, nname);
	MenuMain();
}


from the Header
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#ifndef team_H
#define team_H

class Team
{
public:
	void NewTeam(string, string);
		//function to add a team to the array

...

private:
	int tnumber;
	string tcity;
	string tname;
	
};

#endif 


and from the implementation file
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

#include "team.h"

using namespace std;

int trow;
Team teamlist[31];

void Team::NewTeam(string city, string name)
{
	for (trow=1; teamlist[trow].tnumber != 0; trow++);
		{
		teamlist[trow].tnumber = teamlist[trow-1].tnumber+1;
		teamlist[trow].tcity = city;
		teamlist[trow].tname = name;
		}
}
Any reason why std::string isn't working?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <string>


std::string myfunction()
{
  return "This is a function returning a string";
}

void anotherfunction(const std::string& str)
{
  std::cout << "This function is taking a string as a parameter:  " << str;
}

class MyClass
{
  // this is a class that has a string for a data member,
  //  and an array of 5 strings for another member:

  std::string mystring;
  std::string myarrayofstrings[5];
};


EDIT:

I don't see anything in your code posted that would cause an error. What errors are you getting?

(Although why are you making all your vars global?)
Last edited on
My guess is that the error is caused by this line:

Team::NewTeam(ncity, nname);

Your compiler is probably complaining that you are trying to use a member function without an instantiated object.

Aside from that, I believe you can design the whole thing better. For example, you could use a stl container (vector perhaps) to hold the teams. This helps you in many ways.

-> First, you don't have to write a member function to add objects to an external (to the class) array. All you'll have to do is write a constructor which you'll call as you push_back your objects to the vector.

-> Second, a vector is using dynamic memory allocation, so you don't have to worry that you'll waste memory if you have less than 31 teams or that you won't have enough space if you have more than 31 teams.

-> Third, there's a whole bunch of stl algorithms to help you with whatever operation you want to perform on your data (e.g. sorting by name or city, finding a team with a specific property etc...)

Finally, as Disch hinted too, try avoiding global vars. You can almost always do what you want with local variables passing them as arguments from one function to another.

Here's a small example:

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
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

struct Person
{
    string name;
    int age;

    Person(string name_,int age_):
        name(name_),age(age_){}
};

bool SortByName(const Person & p1, const Person & p2)
{
    return p1.name<p2.name;
}

bool SortByAge(const Person & p1, const Person & p2)
{
    return p1.age<p2.age;
}

void init_db(vector<Person> & db)
{
    db.push_back(Person("John",20));
    db.push_back(Person("Ben",30));
    db.push_back(Person("Mary",40));
    db.push_back(Person("Kate",35));
}

void print_db(const vector<Person> & db)
{
    for (int i=0; i<db.size(); i++)
    {
        cout << "name: " << db[i].name;
        cout << " | age: " << db[i].age;
        cout << endl;
    }
}

int main()
{
    vector<Person> database;

    cout << "initializing database..." << endl;
    init_db(database);

    cout << "\nunsorted database..." << endl;
    print_db(database);

    cout << "\nsorted by name..." << endl;
    sort(database.begin(),database.end(),ptr_fun(SortByName));
    print_db(database);

    cout << "\nsorted by age..." << endl;
    sort(database.begin(),database.end(),ptr_fun(SortByAge));
    print_db(database);

    cout << "\nhit enter to quit...";
    cin.get();
    return 0;
}

Useful links:

http://cplusplus.com/reference/stl/vector/
http://cplusplus.com/reference/algorithm/sort/
Thanks for all the tips guys.

I had looked briefly at the idea of doing this with vectors. But to be honest, I don't know much about them, so I went the array direction instead. Maybe I should rethink that.

As for my tendency to use globar variables? I suspect that's just a bad habit that I've picked up, and need to get out of.

This particular project is just for me. I'm taking my second C++ class this summer (before last spring, I hadn't written a line of code since putting a dot on the screen of an Apple II 25+ years ago) and have not had a particularly helpful teacher. So I'm trying to do some things on my own, and this is the sort of stuff I run in to.

I don't have my editor/compiler in front of me, so I don't have the exact wording of the error I was getting. But I'll take a look when I get home, and see what I can find.

I'll also start looking at the vector section of my books, and try to figure that out.

Thanks again!
Topic archived. No new replies allowed.