0

below is my code which tries to separate a string "key=value" into two subarrays "key" and "value" and here are the errors:

string2StringPair.cc:9:3: error: ‘std’ does not name a type; string2StringPair.cc:10:3: error: ‘std’ does not name a type; string2StringPair.cc:13:12: error: expected initializer before ‘string2StringPair'

#ifndef __PARSE_H_
#define __PARSE_H_

#include "cppstd.hh"
#include <string>
using namespace std;

struct StringPair{
  std:string key; 
  std:string value;
}

StringPair string2StringPair (char* str[]){
  std:string x, y;
  x = ""; y = "";
  for (int i=0;i<str.length();i++){
    if str[i]=="="{
        for (int j=0;j<i;j++){
      x=x+str[j];
    }
        for (int k=(i+1);k<str.length();k++){
      y=y+str[k];
    }
        break; 
    }
  }
  if ((x=="") && (y=="")){
    cout<<"ERROR: There is no = in the input string!"<<endl;
  }

  StringPair tmp;
  tmp.key = x; 
  tmp.value = y;
  return tmp;
} 

#endif

int main(int argc, char *argv[]){
  StringPair pair;
  pair.string2StringPair(argv[1]);
  cout<<"The pair is "<<pair<<endl;
  return 0;
}

would really appreciate if you could help me fix the errors.

When I changed to

std::string key; 
std::string value;

There was no more "std" error. Why??

Why is an initializer expected before string2StringPair? I though I already had one: StringPair?

user2969181
  • 59
  • 2
  • 4
  • 1
    `std::string`, not `std:string`. And I would suggest not using `using namespace std;` at all. – lapk Nov 10 '13 at 19:06
  • This isn't the problem, but names that contain two consecutive underscores (`__PARSE_H_`) and names that begin with an underscore followed by a capital letter are reserved to the implementation. Don't use them. – Pete Becker Nov 10 '13 at 19:07
  • @PeteBecker Actually, it **is** one of the problems -- any program using those (outside the implementation) has undefined behavior. –  Nov 10 '13 at 19:15
  • @H2CO3 - it is **not** the problem here. Undefined behavior does not mean that something bad must happen. The problem, as others have indicated, is a missing ':'. – Pete Becker Nov 10 '13 at 19:23

4 Answers4

4

You missed this one :

std:: instead of std:

EDIT

When I changed to

std::string key; std::string value; There was not more "std" error. Why??

Because C++ compiler expects another : after : as per definition of the scope resolution rules in C++ standards.

Aniket Inge
  • 25,375
  • 5
  • 50
  • 78
2

The separator between namespace and what follows is two colon characters in C++.

So, you'd need the following:

std::string key;

Or, since you're saying using namespace std, you could actually omit the std:: prefix altogether. However, using namespace std is not considered good coding practice.

Long story short: Remove the using namespace std; and use std:: prefix.

Also, for the include guards, don't use identifiers with double underscore (or even single underscore). There's quite strict rules about leading underscores in identifiers being reserved in the C++ standard. While you might get away when using them, it's definitely not recommendable.

Just use

#ifndef PARSE_H
#define PARSE_H
Community
  • 1
  • 1
codeling
  • 11,056
  • 4
  • 42
  • 71
1

std:: and not std: //note :: is two times.

If you are using namespace std, there is no need for using "std::" anyway. Choose one convention of programming.

As to your edited question, of why there is no more error, after using:: ,

"::" is used for accessing static variables and methods of a class/struct or namespace. It is also commonly used to access variables and functions from another scope. Brush up on the basics of C++, else it be tougher the more you progress.

rockinfresh
  • 2,068
  • 4
  • 28
  • 46
1

There are a number of errors.

You need to use two colons after std where you're using it.

struct declarations need to be followed by a semicolon.

struct StringPair {
    std::string key;
    std::string value;
};  // <-- add semi-colon here

You meant to use std::string as the input to string2StringPair() since you use str like an object in that function.

StringPair string2StringPair (std::string str){

You need to use str.size() to get the length of a string, and character comparison use single quotes, not double quotes.

for (int i=0 ; i< str.size();i++){  // use str.size() 
    if(str[i] == '=') {          // "=" is a string.  '=' is a single character.

In your main() function, you meant to assign the result of string2StringPair() into pair. pair doesn't have any methods to call.

pair = string2StringPair(argv[1]);
Phillip Kinkade
  • 1,382
  • 12
  • 18