-1

Okay, im making a pretty big file in my opinion, so i wanted to separate it into several files for cleaner code. so i have my main .cpp file and two header files holding my classes. well the header files dont hold strings, it aboslutely wont budge. i call the library in both my .cpp file and even tried it in my header file.

another issue i ran into is using strings to make switches function, reason being if i use integers in a switch if the user inputs a alphabetical character the program goes into an endless loop.

string choice;

switch (choice)
{

  case "1" : 
  //... 
  break;
  case "2" : 
  //... 
  break;
}

and my last issue is when i create an object in a case it gives an error. says cross initialization of object.

string choice;

switch (choice)
{

  case "1" : 
  Class object; 
  break;
  case "2" : 
  //... 
  break;
}

Here is the header issue im having. ///main.cpp////

#include <iostream>
#include <string>
#include "customer.h"

//// customer.h ////

class Customer
{

string name;      
string meal;
// method

public:
int Choose_cCustomer()
{
 int a;   
 a = rand () % (10 - 1 + 1) + 1;
 return a;   


};

complier code : 'string' does not name a type;

TimothyTech
  • 745
  • 2
  • 11
  • 18
  • Nobody's answered your first question; it's unclear what you mean by it, at least to me. Could you post sample code and your compiler error that explains what you mean by "the header files don't hold strings?" – Jacob Jun 17 '10 at 17:16
  • 1
    Not sure at all what you mean in your first paragraph. The header files "don't hold strings"?? – Paul Richter Jun 17 '10 at 17:16
  • alright i added the code to the first paragraph – TimothyTech Jun 17 '10 at 17:20
  • You have a lot of fundamentals missing, why not grab one of these? http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – GManNickG Jun 17 '10 at 17:58
  • @TimothyTech : I revised my answer to include your string problem. – Stephen Jun 17 '10 at 17:58
  • you seem to be missing an '}' for Choose_cCustomer(). – Stephen Jun 17 '10 at 18:19

4 Answers4

7

"string" does not name a type

Add #include <string> at the top of your header file, since it is used in the header file, it must be included first. Since string is defined in the std namespace, you should declare it with std::string name;.

In the cpp file, you can shortcut with using namespace std;, but it might be best practice to always refer to the qualified name (the "qualified name" includes the namespace - e.g. std::string or std::vector).

I cannot do switch(string)

That is correct, switches are reserved for "integral values". Or values that can be treated as integral (e.g. characters). See (http://www.cprogramming.com/tutorial/lesson5.html)

I cannot do case 1: Class object;

That is sort-of correct. A case cannot directly have variables declared in it. However, there is a quick workaround:

case 1: {  // Notice the added braces, to create a 'scope' for which to define object.
  Class object;
  // ... use object as normal ...
  break;
}

If you really want to compare strings, you should chain if () { } else if () { } else { } statements.

Stephen
  • 47,994
  • 7
  • 61
  • 70
  • Do you know if C++0x will allow you to switch on a constexpr? – Cogwheel Jun 17 '10 at 17:44
  • @Cogwheel: What do you mean? A case could be a constant-expression, yes. (It is right now, all `constexpr` does is allow that to be put in a function.) – GManNickG Jun 17 '10 at 17:54
  • @TimothyTech : Oh, you're missing the `std::`. Try that. – Stephen Jun 17 '10 at 18:21
  • oo okay, ill try that. would "using namespace std;" work the same in the header file? – TimothyTech Jun 17 '10 at 18:33
  • @TimothyTech : It will "work", but at some point it will break something. It's not a good idea to put using directives in the global part of the header file (see http://nepsweb.co.uk/pgtcpp/namespace.htm ). You can put it within the class... but as I said - it ain't a bad thing to be explicit :) – Stephen Jun 17 '10 at 18:37
  • @GMan: erm yeah, that should've read "Since C++0x has constexpr, will we be allowed to switch on non-integral classes?" I was previously under the impression that `switch` wasn't usable on custom types because there was no way to build a compile-time constant for the `case`s. – Cogwheel Jun 17 '10 at 18:52
  • i think something is wrong with my compiler. i remade the program from the ground up and now in the header file it is saying cout cin and endl are undeclared... is there a more efficent way to divide code? lol – TimothyTech Jun 17 '10 at 18:53
  • @Cogwheel : I haven't seen anything to that effect (even after scanning some active proposals) but that doesn't mean it isn't happening... – Stephen Jun 17 '10 at 19:01
  • @TimothyTech : `cout`, `cin`, and `endl` are all within the std namespace. Move that code from the header to the .cpp and add `using namespace std;` :) And don't worry, at some point this stuff will make sense and programming will get more interesting! – Stephen Jun 17 '10 at 19:02
  • alrighty, that worked. thank you very much. This stuff is so much more intense than PHP. lol. – TimothyTech Jun 17 '10 at 19:32
0

No, you can't do that - so don't do it - use an if-ladder instead. switches can only be performed on integer types, and are often overused even then, IMHO.

Your third issue is because cases do not create a scope:

#include <string>
using namespace std;

int main() {
    int n = 0;
    switch( n ) {
        case 0: 
           string s1;

        case 1: 
           string s2;

    }
}

If n has the value 1, the creation of s2 gets skipped over, which is illegal in C++. You need to set up scopes yourself:

int main() {
    int n = 0;
    switch( n ) {
        case 0: {
           string s1;
        }
        case 1: {
           string s2;
        }
    }
}

But I should say that I consider that any case that contains more than a single statement, plus maybe a break, would be bad practice.

0

A switch statement works with integral values only. Integral values include char but exclude pointers, and the type of a literal like "1" is const char *.

Instead, use '1' and the like. '1' is an int that holds the character value of '1'. Single quotes, not double.

In order to do this, you'll have to switch on choice[0] rather than choice, since you have to get a char.

David Thornley
  • 56,304
  • 9
  • 91
  • 158
0

As regards the compiler error you need to put the line

#include <string>

in customer.h. Putting it in main.cpp before including customer.h will only work when compiling main.cpp, and even then only provided customer.h doesn't get indirectly included by an earlier header. When you compile customer.cpp then it will fail.

Edit:

As @Mike Seymour points out you'll need to use std::string everywhere or add the using declaration using std::string after including the string header. I got the impression your code was working until you split it up into different files so I assume you already have the using declaration further down in main.cpp.

Troubadour
  • 13,334
  • 2
  • 38
  • 57