Solved

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

Posted on 2008-11-02
4
422 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 31

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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In our object-oriented world the class is a minimal unit, a brick for constructing our applications. It is an abstraction and we know well how to use it. In well-designed software we are not usually interested in knowing how objects look in memory. …
Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

717 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