2

Code:

#include <vector>
#include <iostream>

typedef struct
{
    std :: string latitude;
    std :: string longitude;
} coordinate;

std :: vector <coordinate> previousPoints;

int main ()
{
    coordinate.latitude  = latitude;
    coordinate.longitude = longitude;
    previousPoints.push_back (coordinate);

    return 0;
}

Output:

anisha@linux-y3pi:~> g++ -Wall demo.cpp
demo.cpp: In function ‘int main()’:
demo.cpp:14:12: error: expected unqualified-id before ‘.’ token
demo.cpp:15:12: error: expected unqualified-id before ‘.’ token
demo.cpp:16:38: error: expected primary-expression before ‘)’ token

What's the point that I am missing?

Aquarius_Girl
  • 21,790
  • 65
  • 230
  • 411

3 Answers3

4
typedef struct
{
    std :: string latitude;
    std :: string longitude;
} coordinate;

coordinate is typedef on anonymous struct, not object. You should create object of coordinate in your function, or not use typedef, i.e.

struct coord
{
    std :: string latitude;
    std :: string longitude;
} coordinate;

now, coordinate is object. And one question, why you type spaces after std and after ::? It's legal, but strange.

ForEveR
  • 55,233
  • 2
  • 119
  • 133
  • I had tried these both ways, didn't work. The fault has been identified by YePhlck. Space after :: makes the code look "clean" to "me". – Aquarius_Girl Aug 04 '12 at 11:19
  • @AnishaKaul Ehm... First way is create object of coordinate in main (YePhlck suggest this way), second way - coordinate is global object of type coord... – ForEveR Aug 04 '12 at 11:22
  • It's almost never a good idea to have globals, so I'd refrain from suggesting this as a solution, TBH – YePhIcK Aug 04 '12 at 11:23
  • @YePhIcK Yes, global objects is evil, but sometimes - it's okay, so, i suggest first solution, since OP's code looks like using global object. – ForEveR Aug 04 '12 at 11:28
4

You need to create an actual variable to be added to your vector:

int main ()
{
    coordinate c;
    c.latitude  = latitude;
    c.longitude = longitude;
    previousPoints.push_back (c);
YePhIcK
  • 5,816
  • 2
  • 27
  • 52
  • I can't believe this!! wow, sometimes I can be so forgetful, thanks, – Aquarius_Girl Aug 04 '12 at 11:17
  • 2
    Some programming styles impose certain letter capitalizations to avoid these sorts of issues. For example if you were to **always** name your data types with a leading capital letter and **never** use a leading capital letter for your identifiers you'd've avoided this problem while typing the code in – YePhIcK Aug 04 '12 at 11:20
  • YePhlck, I use "camelBack", that couldn't have saved me here ;). @ForEveR Besides this is just a part of reproducible example that I created to post here. So, of course the actual file won't have globals. – Aquarius_Girl Aug 04 '12 at 11:32
  • 2
    @AnishaKaul `coordinate` should have been `Coordinate`. – juanchopanza Aug 04 '12 at 12:26
  • @juanchopanza camelBack style doesn't allow that. I;ll see what more styles are there, though. – Aquarius_Girl Aug 04 '12 at 13:00
  • You can prefix your types with a `t_` or something similar to that – YePhIcK Aug 04 '12 at 17:54
1
coordinate.latitude  = latitude;

You need an object to access a members of it, but coordinate just names a struct. Also on the right hand side you don't have a value... You need something like:

coordinate c;
c.latitude  = "120";
c.longitude = "10";
previousPoints.push_back(c);
Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176