0

The call that generates the unresolved external symbol:

#include <string.h>
#include "GContext.h"
#include "GBitmap.h"
#include "GColor.h"

int main(int argc, char** argv) {
    const int W = 100;
    const int H = 100;
    GContext* ctx = GContext::Create(W, H);

The abstract class method signature:

#ifndef GContext_DEFINED
#define GContext_DEFINED

#include "GTypes.h"

class GBitmap;
class GColor;

class GContext {
public:
    GContext() {}
    virtual ~GContext() {}


    virtual void getBitmap(GBitmap*) const = 0;

    virtual void clear(const GColor&) = 0;


    static GContext* Create(const GBitmap&);

    static GContext* Create(int width, int height);
};

#endif

And the Current Derived Class Implementation and Method Signature:

#include "GColor.h"
#include "GPixel.h"
#include "GBitmap.h"
#include "GContext.h"
#include "GTypes.h"
class myGContext : public GContext
{
public:
        myGContext() : GContext(){}
        static const GBitmap* bitmap;

        void getBitmap(GBitmap* bitmap) const
        {

        }

        void clear(const GColor& gcolor)
        {
        int length = sizeof( (GPixel)(bitmap->fPixels) ) / sizeof(GPixel);
        for (int i = 0; i < length; i++)
        {
            (bitmap->fPixels)[i]
        }

        }

        static GContext* Create(const GBitmap& gbitmap)
        { 
        GContext::Create(gbitmap);
        bitmap = &gbitmap;
        GContext* g = new myGContext();
        return g;
        }


        static GContext* Create(int width, int height)
        {
        GContext::Create(width,height);
        GContext* g = new myGContext();
        return g;

    }
};

So I understand that I need to define both types of the function GContext::Create() to resolve the external symbol error, but I need to define them in my derived Class. Which I thought I was doing right, any ideas?

3 Answers3

0

no Inheritance does not work, this is not like a virtual function.

dzada
  • 5,344
  • 5
  • 29
  • 37
0

I'm not exactly sure what you're trying to do but if you

  • Need to have static functions
  • Need both base and derived classes to have their own implementation
  • derived needs access to the base class' functions

this is all achievable:

#include <iostream>

class A {
public:
    A() {}
    static void f() { std::cout << "A f" << std::endl; }
};

class B : public A {
public:
    B() {}
    static void f() { std::cout << "B f" << std::endl; }
};




int main(int argc, char* argv[]) {

    A a;
    B b;

    a.f();
    b.f();
    b.A::f();
    return 0;
}

Output is

A f
B f
A f
Chemistpp
  • 2,006
  • 2
  • 28
  • 48
0

I think it is just because you static method is not defined in your base class. From here it is said that LNK2019 can also occur when a static data member is declared but not defined.


Also, be careful when you try to redefine static methods inside subclasses:

You cannot override a static method in a subclass, you can only hide it.

And from the C++ standard:

9.4.1 Static member functions [class.static.mfct]

2/ A static member function shall not be virtual. There shall not be a static and a non-static member function with the same name and the same parameter types (13.1). A static member function shall not be declared const, volatile, or const volatile.

Example:

#include <iostream>

class Foo
{
public:
  static void func() { std::cout << "Foo::func" << std::endl; }
};

class Bar : public Foo
{
public:
  static void func() { std::cout << "Bar::func" << std::endl; }
};

int main(void)
{
  Foo::func();     // Works
  Bar::func();     // Works

  Foo foo;
  Bar bar;

  foo.func();        // Works
  bar.func();        // Works
  bar.Foo::func();   // Works

  Foo* foobar = new Bar;

  foobar->func();      // Not the result expected
                       // Because no override.
  return 0;
}
Community
  • 1
  • 1
Pierre Fourgeaud
  • 14,290
  • 1
  • 38
  • 62