tags:

views:

147

answers:

4

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;

A: 

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.

anon
yeah i agree, i normal do use if statements but thought id give a switch a world since they look cleaner.
TimothyTech
+7  A: 

"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
Do you know if C++0x will allow you to switch on a constexpr?
Cogwheel - Matthew Orlando
@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.)
GMan
thanks stephen but i tried that too.
TimothyTech
@TimothyTech : Oh, you're missing the `std::`. Try that.
Stephen
oo okay, ill try that. would "using namespace std;" work the same in the header file?
TimothyTech
@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
@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 - Matthew Orlando
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
@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
@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
alrighty, that worked. thank you very much. This stuff is so much more intense than PHP. lol.
TimothyTech
A: 

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
A: 

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
i did to no avail
TimothyTech
You'll also need to give the type its full name, `std::string`.
Mike Seymour
@Mike: Um, yes. Good point!
Troubadour