syntax problem?

20 Sep 2012

the compiler kicks out "undefined symbol mbed::country_count" when I try to compile the following. I thought that I was defining country_count in the countries.h header but apparently not - what am i doing wrong here guys??

//countries.h
#ifndef mbed_countries_H
#define mbed_countries_H
#include "mbed.h"

namespace mbed {

struct country_data {
    const char *pref;
    const char *name;
    short int lat, lon;
};
extern const country_data country_table[];
extern const int country_count;
const country_data country_table[] = {

    {"1A","SMO MALTA",42,13 }, 
    {"1S","SPRATLY IS",9,112 }, 
};
}
#endif
______________________________
//main.cpp
#include "mbed.h"
#include "countries.h"

void dxcc() 
{
    printf("count = %d \n", country_count);
}

int main() 
{
    const int country_count = sizeof(country_table) / sizeof(country_table[0]);
    dxcc();
}

20 Sep 2012

Hi,

The extern keyword declares a variable, but it is not a definition. You have to put a definition of the country_count in mbed namespace. Or, simply remove extern keyword for the country_count with initialization.

namespace mbed {
// snip
const int country_count = 0;
// snip
}

Regards,

20 Sep 2012

thanks for the reply.. I tried removing the extern directive and initialized country_count to 0 as above but the compliler still kicks out the same error (undefined symbol mbed::country_count). Any other ideas guys?

26 Sep 2012

Hmm,

I didn't get undefined symbol error by the change below:

//countries.h
#ifndef mbed_countries_H
#define mbed_countries_H
#include "mbed.h"
 
namespace mbed {
 
struct country_data {
    const char *pref;
    const char *name;
    short int lat, lon;
};
extern const country_data country_table[];
const int country_count = 0;
const country_data country_table[] = {
 
    {"1A","SMO MALTA",42,13 }, 
    {"1S","SPRATLY IS",9,112 }, 
};
}
#endif

26 Sep 2012

Thanks for looking at this again Toy. When I make your change, it will compile but I get a "country-count declared but never referenced" warning. When I run the program, country_count is indeed missing... I think I've looked at every permutation. Interestingly, the following works but I'd like to make the one-time calculation of country_count in main() if possible so that it doesn't have to be made each and every time I call dxcc(). Ideas ?? I'm new and learning a bit every day and this one has me stumped (temporarily)...

//countries.h
#ifndef mbed_countries_H
#define mbed_countries_H
#include "mbed.h"
 
namespace mbed {
 
struct country_data {
    const char *pref;
    const char *name;
    short int lat, lon;
};
extern const country_data country_table[];
extern const int country_count;
const country_data country_table[] = {
 
    {"1A","SMO MALTA",42,13 },
    {"1S","SPRATLY IS",9,112 },
};
}
#endif
______________________________
//main.cpp
#include "mbed.h"
#include "countries.h"
 
void dxcc()
{
    const int country_count = sizeof(country_table) / sizeof(country_table[0]);
    printf("count = %d \n", country_count);
}
 
int main()
{
    dxcc();
}
26 Sep 2012

Hi,

Quote:

When I make your change, it will compile but I get a "country-count declared but never referenced" warning. When I run the program, country_count is indeed missing...

Yes. the country_count in main() was declared as locally and did not have global scope.

Quote:

Interestingly, the following works but I'd like to make the one-time calculation of country_count in main() if possible so that it doesn't have to be made each and every time I call dxcc(). Ideas ??

An initialization of the country_count can be in the header file, since initialization value is constant data. You don't need to calculate this value at the runtime.

//countries.h
#ifndef mbed_countries_H
#define mbed_countries_H
#include "mbed.h"
 
namespace mbed {
 
struct country_data {
    const char *pref;
    const char *name;
    short int lat, lon;
};
extern const country_data country_table[];
const country_data country_table[] = {
 
    {"1A","SMO MALTA",42,13 }, 
    {"1S","SPRATLY IS",9,112 }, 
};
const int country_count = sizeof(country_table) / sizeof(country_table[0]);
}
#endif
______________________________
//main.cpp
#include "mbed.h"
#include "countries.h"
 
void dxcc() 
{
    printf("count = %d \n", country_count);
}
 
int main() 
{
    dxcc();
}

I hope this helps.

26 Sep 2012

Bingo !! You have added to my understanding. Works perfectly. Thanks man and great Wednesday to you.

garyb