-1
4

6 に答える 6

2

The map needs a type for the mapped_type, and you are giving it re instance r. You need

std::map<unsigned int, re> var;

Your code can be simplified further:

struct re {
  std::string s;
};

int main()
{
  re r;
  std::map<unsigned int, re> var;

  r.s="hello";
  var[10]=r;
}
于 2013-03-17T21:11:45.740 に答える
2

r is an instance of the re type, which is a type alias for the struct you define. Therefore, you should change this:

std::map<unsigned int, r> var;

Into his:

std::map<unsigned int, re> var;
//                     ^^

Also notice, that in the main() procedure you are re-defining the re structure, when you most likely just wanted to create an instance of it:

int main() {
    re r;
    r.s="hello";
    var[10]=r;
}

Thus, putting everything together:

#include <string>
#include <map>

// Avoid this old C style of defining structs. Prefer the
// C++ style I am showing below...
/*
typedef struct {
      std::string s;
  } re;
*/

struct re
{
    std::string s;
};

int main() {
    std::map<unsigned int, re> var;
    re r;
    r.s="hello";
    var[10]=r;
}
于 2013-03-17T21:12:01.590 に答える
1

First, you can just say

struct re { std::string s; };

in C++. The typedef struct {...} name; is a C-ism.

In your map, you must give a type instead of a variable

std::map<unsigned int, re> var;

The error you get in the main() function, is because you try to assign a variable of a different type main()::struct re to the map element 10, which is of type typedef struct { ... } re.

To summarize, struct re in main is not the same type as the typedef ... re in the global scope.

于 2013-03-17T21:12:23.707 に答える
1

please change

std::map<unsigned int, r> var;

into

 std::map<unsigned int, re> var;

you define map of some type, in <> you passes types that you want map to "map" (define mapping between them).

This is all code snippet:

#include <map>
typedef struct {
    std::string s;
}re;
re r;
std::map<unsigned int, re> var;

int f(int argc, char** argv) {
   re res; 
   res.s="hello";
   var[10]=res;
   //and if you want that global variable
   r.s="global variable";
   var[1]=r; 

   return ERROR_SUCCESS;
}
于 2013-03-17T21:14:07.973 に答える
1

Comments inline describe how to fix your code.

#include <string>
#include <map>

//typedef struct { // This is from C. Use the C++ way...
struct re_type {
    std::string s;
//} re; // See above
    };

// re r; // You do not need a global instance

// std::map<unsigned int, r> var; // `r` was an instance.
// The map declaration needs a type:
std::map<unsigned int, re_type> var; // Note: This is global and should probably be 
                                     // moved into main()

int main(){
    // struct re{  // No need to redeclare the struct here
    //    string s;
    // }re;

    re_type re; // Create a local instance

    re.s="hello";
    var[10]=re;
}

re, r, s and var do not appear to be descriptive identifiers. Using meaningful identifiers will make your code easier to read and maintain and will even help you to understand compilation errors as they arise.

于 2013-03-17T21:27:45.683 に答える
0
typedef struct {
    std::string s;
} re;

Then later:

std::map<unsigned int, r> var;

Nah. The template needs the type but you're giving it a variable. Change it to

std::map<unsigned int, re> var;

and it will work fine.

于 2013-03-17T21:12:45.670 に答える