0

I'm having some problems with this map I set-up. I'm checking "data" for any matches against the strings in mymap. The map in a seperate header which is called from another header. In any case I'm getting these errors. on the "mymap" line I get "this declaration has no storage class or type specifier. On the "map"portion I get initialization with'{,,,}' exprcted for aggregate object. And finally on the "for" I get "expected a declration". I'm using Visual Studio 2010 and Windows 7. Here is the code......

#include <stdio.h>
#include <iostream>
#include <map>
using namespace std;

void printContent(int)
map <int,string> mymap;
int i;
mymap [1]="audio/basic";
mymap [2]="audio/x-aiff";
mymap [3]="audio/x-wav";
mymap [4]="audio/x-mpeg";
mymap [5]="audio/x-mpeg-2";

for i=0 to map.count-1
  {
      char s= strstr(data, map[i])
      if s != NULL; // you found a match
      {
          return 1;
      }
      {
      else // keep looping
      }    
      return 0;
  }
Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
ME-dia
  • 289
  • 1
  • 5
  • 18
  • 1
    Welcome to Stack Overflow! Please don't forget to read the [FAQ](http://stackoverflow.com/faq#howtoask) to help you get started here. – Jacob Sep 06 '11 at 17:58
  • Also, please try to post a section of code which reproduces the bug you are facing ; it'll make it much easier to pin-point the exact cause. – Jacob Sep 06 '11 at 18:00
  • 3
    Where did you read that you could write a for loop like that in C++? Did you really paste the actual code you wrote or did you strip out some of the `{` and `}`? – Mat Sep 06 '11 at 18:00
  • 1
    Please read a [book](http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) about C++ :) There are too many things to fix here so that we can give you useful advice: first learn the syntax, then learn about `std::string`. If you have any question in the process, we'll be glad to help. In the meantime, I vote to close this one. – Alexandre C. Sep 06 '11 at 18:12
  • I gave him the answer. Maybe this question should be closed as not useful though. – N_A Sep 06 '11 at 18:15
  • Why would you close a question where someone needs help? If you don't want to help, fine. But don't take away my chance to resolve the issue. – ME-dia Sep 06 '11 at 18:24
  • This question should be closed once your issue is resolved because it won't be helpful to anyone else and it will just clutter up SO. – N_A Sep 06 '11 at 18:44
  • Your brace line up is messed up. Your "else" clause is inside a new statement, without an associated `if`. I believe you want to rewrite it. – Thomas Matthews Sep 06 '11 at 19:44

3 Answers3

1

Here's a working, compileable example which does something along the lines of what I think you're trying to do. Does this help?

#include <iostream>
#include <map>
#include <string>

template <typename Map, typename Value>
typename Map::const_iterator find_value(const Map& m, const Value& v)
{
    for(typename Map::const_iterator it=m.begin(), iend=m.end(); it!=iend; ++it)
    {
        if(it->second == v) // do whatever test you like here
        {
            return it;
        }
    }
    return m.end();
}

int main()
{
    std::map<int,std::string> mymap;
    mymap[1] = "audio/basic";
    mymap[2] = "audio/x-aiff";
    mymap[3] = "audio/x-wav";
    mymap[4] = "audio/x-mpeg";
    mymap[5] = "audio/x-mpeg-2";
    std::cout << find_value(mymap, "audio/x-aiff")->first << '\n';
    return 0;
}
Stuart Golodetz
  • 20,238
  • 4
  • 51
  • 80
  • Thank you Stuart! This is good! I tried to give you points but my repetation is to low since I just started. – ME-dia Sep 06 '11 at 22:27
0

Try

void printContent(int)
{
    map <int,string> mymap;
    int i;
    mymap [1]="audio/basic";
    mymap [2]="audio/x-aiff";
    mymap [3]="audio/x-wav";
    mymap [4]="audio/x-mpeg";
    mymap [5]="audio/x-mpeg-2";

    for(i=0; i < map.count-1; ++i)
      {
          char s= strstr(data, map[i])
          if(s != NULL) // you found a match
          {
              return 1;
          }
          else
          {
             //Do something here
          }
      }
      return 0;
}
N_A
  • 19,799
  • 4
  • 52
  • 98
  • I haven't used C++ in quite some time, but I'm pretty sure it doesn't have a `for i to count` type construct. – N_A Sep 06 '11 at 18:08
  • That's the right idea, but all that code (as written by the asker) is outside of a function... – Mat Sep 06 '11 at 18:10
  • It looks like you're trying to write things as you might in vb? You needed a LOT more of these things - '{' '}' – N_A Sep 06 '11 at 18:10
  • @Mat Thanks, missed that he also didn't have parenthesis around his function body. – N_A Sep 06 '11 at 18:14
  • Thank you, it's getting closer. It's saying std::map is missing. and no instance of overload function "strstr" matches the overload function. – ME-dia Sep 06 '11 at 18:22
  • 1
    I fixed the basic scoping issues, but if you can't find issues like this: `for(i=0; i < map.count-1; ++i)` <-should be mymap not map, then you should go back and learn the basics. – N_A Sep 06 '11 at 18:47
0

You are accessing a map location that has no value.

In the first iteration of the for loop, when i is set to zero, your program accesses map[0], which you haven't assigned any values to.

Either assign a value to map[0] or change your map index to map[i+1].

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154