Solved

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

Posted on 2008-11-02
4
417 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 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VS2008 C++ memory leaks and how to fix them 7 192
I can't set a breakpoint ? 5 113
Display current date,month,and year to JTextField 3 48
populate value from mongo db in java 98 317
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…
Here is a helpful source code for C++ Builder programmers that allows you to manage and manipulate HTML content from C++ code, while also handling HTML events like onclick, onmouseover, ... Some objects defined and used in this source include: …
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.

896 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

11 Experts available now in Live!

Get 1:1 Help Now