开发者

I can't put a string in a switch nor an array in a class

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 alphabeti开发者_开发问答cal 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;


"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.


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.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜