Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

compilation error in Code Blocks and it runs normally in visual studio

27 views
Skip to first unread message

Ab

unread,
Oct 12, 2016, 10:51:29 PM10/12/16
to
template <typename elementType = int, int numberOfElements = 5>
class Array
{
//some code
private:
// the number of objects currently instantiated
static size_t arrayCount;
};


// class Array_include.h
#include "Array.h" // Array class definition
using namespace std;

// initializing the template static member arrayCount
template <typename elementType, int numberOfElements>
size_t Array<elementType, numberOfElements>::arrayCount = 0;

// ^^^ code block (using GCC compiler) gave me compilation error, and in
//visual studio it worked without any issues, does anyone know why?

Ian Collins

unread,
Oct 13, 2016, 12:11:03 AM10/13/16
to
On 10/13/16 03:49 PM, Ab wrote:
> template <typename elementType = int, int numberOfElements = 5>
> class Array
> {
> //some code
> private:
> // the number of objects currently instantiated
> static size_t arrayCount;
> };
>
>
> // class Array_include.h
> #include "Array.h" // Array class definition
> using namespace std;

In a header?? No!

> // initializing the template static member arrayCount
> template <typename elementType, int numberOfElements>
> size_t Array<elementType, numberOfElements>::arrayCount = 0;
>
> // ^^^ code block (using GCC compiler) gave me compilation error, and in
> //visual studio it worked without any issues, does anyone know why?

What was the error?

--
Ian

mark

unread,
Oct 13, 2016, 4:47:55 AM10/13/16
to
You should post the error. Maybe an include for size_t is missing. It's
used in quite a few headers, so an explicit include is usually not needed.

Alf P. Steinbach

unread,
Oct 13, 2016, 10:40:23 AM10/13/16
to
On 13.10.2016 04:49, Ab wrote:
> template <typename elementType = int, int numberOfElements = 5>
> class Array
> {
> //some code
> private:
> // the number of objects currently instantiated
> static size_t arrayCount;
> };

This will give you one `arrayCount` for each used combination of
template parameter values.


> // class Array_include.h
> #include "Array.h" // Array class definition
> using namespace std;

Don't ever do this (in the global namespace) in a header.


> // initializing the template static member arrayCount
> template <typename elementType, int numberOfElements>
> size_t Array<elementType, numberOfElements>::arrayCount = 0;
>
> // ^^^ code block (using GCC compiler) gave me compilation error, and in
> //visual studio it worked without any issues, does anyone know why?

Readers can /guess/ that you've not include a header that defines
`size_t` in the global namespace.

The primary such header is `<stddef.h>`.

But really, why t.f. are you posting without quoting the error message,
and without a complete, minimal example that reproduces the error?

• • •

See the old FAQ's “How do I post a question about code that doesn't work
correctly?” e.g. as mirrored at <url:
http://www.dietmar-kuehl.de/mirror/c++-faq/how-to-post.html#faq-5.8>


Cheers, & hth.,

- Alf

Louis Krupp

unread,
Oct 13, 2016, 12:44:50 PM10/13/16
to
It's all part of the learning process. Brevity might be the soul of
wit, but it needs to be fine-tuned to the situation. It took me a
while to catch on.

Louis
0 new messages