Solved

Modular C++, class with strings.. not working very well for me.

Posted on 2008-11-02
4
416 Views
Last Modified: 2013-12-14
Yes, this is my homework. No, I'm not asking for a solution to it, just some help (I'm getting frustrated with google, and the deadline is midnight). I just need some help on how I do this.

So I've got 3 files, main.cpp (the main program file), team.h, and team.cpp.

I'm supposed to create a class that stores some data and has some functions, using the modular files team.h and team.cpp.

My main.cpp file is something like this:
---------------------------
#include <iostream>
#include <string>
#include "team.h"
using namespace std;

int main()
{.... return blah;}

---------------------------------

Then I'm at a loss for what to do with my team.h and team.cpp files.

Team.cpp has:
---------------------------
#include "team.h"

Team.h has:
#include <string>

class Team
{
      string tName;
      string tCon;
      string tDiv;

      void Team::Load(string sValue)
      {
            tName=sValue.substr(0,sValue.find(' '));
            tCon=;
            tDiv=;
      }

      
      int x;
};

-----------------

When I change the variable types in this class to char* (or any other type), that part works.

So I would like to know why it's not working when I use string as my data type for this, and what I can do to fix that. I think that will help me fix the rest of my problems.

Again, since this is homework, an explanation of what is done would be appreciated.

Thanks!
0
Comment
Question by:brian-jg
  • 2
4 Comments
 
LVL 2

Author Comment

by:brian-jg
ID: 22863144
AAaaand I'm a fool.

It's because I wasn't following namespace rules.

Points go out to whoever describes how to set up these .h files while paying attention to namespace.


/exams = braindead, go me.
0
 
LVL 30

Expert Comment

by:Zoppo
ID: 22931094
Hi brian-jg,

you just need to put a 'using namespace std;' before the first use of 'string' - sthis is in 'team.h'. Modify it i.e. like this:

> #include <string>
> using namespace std;
> class Team
> ...

With this you don't need the 'using namespace std;' in main.cpp anymore ...


Another possibility is to add 'std::' as prefix to all occurances of 'string', i.e.:

> #include <string>
>
> class Team
> {
>  std::string tName;
>  std::string tCon;
>  std::string tDiv;
>
>  void Team::Load(std::string sValue)
>  {
> ...

It's hard to say which way is better ... I like to use the second approach, because doing a global 'using namespace ...' may collide with other code, i.e.:

> using namespace std;
> char* string;
> string x;

would lead to an error while

> char* string;
> std::string x;

won't - of course this is a bad sample since it's not a good practice to name a variable 'string' - but there are cases (i.e. when using third-party libraries) in which you can get such name collisions.


Hope that helps,

ZOPPO



0
 
LVL 39

Expert Comment

by:itsmeandnobodyelse
ID: 22953577
You shouldn't use the 'using namespace std' in the header in order to make your header includable together with headers using a different namespace.

Moreover, any header needs to include all the headers it needs. That means a header should not rely on that the cpp which included it has made some necessary includes *before*.  

For your case it means:

0
 
LVL 39

Accepted Solution

by:
itsmeandnobodyelse earned 220 total points
ID: 22954051
@!*=§!!+***@

I forgot to check the 'attach code snippet' ...

Again:
 
// team.h

#ifndef TEAM_H  // that makes the header to not be included twice

#define TEAM_H
 

#include <string>
 

class Team

{

      std::string tName;

      std::string tCon;

      std::string tDiv;
 

      void Team::Load(std::string sValue)

      {

            tName=sValue.substr(0,sValue.find(' '));

            tCon=;

            tDiv=;

      }
 

      

      int x;

};
 

#endif // #ifndef TEAM_H
 

// team.cpp

#include "team.h"  // it now doesn't matter that it is the first #include
 

#include <iostream>

#include <string>

using namespace std; // here it is ok cause you know that 

                     // you don't include a concurrent namespace
 

int main()

{

     string any;   // no std:: prefix required

Open in new window

0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org (http://seleniumhq.org) Go to that link and select download selenium in the right hand columnThat will then direct you to their downlo…
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now