Solved

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

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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

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: …
Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
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 use and create keystrokes in Netbeans IDE 8.0 for Windows.

786 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