C++ C++;-LNK 2019错误,试图定义子类静态函数
生成未解析的外部符号的调用:C++ C++;-LNK 2019错误,试图定义子类静态函数,c++,function,static,subclass,lnk2019,C++,Function,Static,Subclass,Lnk2019,生成未解析的外部符号的调用: #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); 以及当前派生类实现和方法签名: #inc
#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);
以及当前派生类实现和方法签名:
#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;
}
};
#包括“GColor.h”
#包括“GPixel.h”
#包括“GBitmap.h”
#包括“GContext.h”
#包括“GTypes.h”
类myGContext:公共GContext
{
公众:
myGContext():GContext(){}
静态常量GBitmap*位图;
void getBitmap(GBitmap*位图)常量
{
}
空隙清除(常量颜色和颜色)
{
int length=sizeof((GPixel)(位图->fPixels))/sizeof(GPixel);
for(int i=0;ifPixels)[i]
}
}
静态GContext*创建(常量GBitmap和GBitmap)
{
GContext::创建(gbitmap);
位图=&gbitmap;
GContext*g=新的myGContext();
返回g;
}
静态GContext*创建(整数宽度、整数高度)
{
GContext::创建(宽度、高度);
GContext*g=新的myGContext();
返回g;
}
};
所以我知道我需要定义两种类型的函数GContext::Create()来解决外部符号错误,但我需要在派生类中定义它们。我认为我做得对,有什么想法吗?没有继承不起作用,这不像是一个虚拟函数。我不太确定你想做什么,但是如果你
- 需要有静态函数
- 需要基类和派生类都有自己的实现
- 派生类需要访问基类的函数
#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;
}
我认为这只是因为在基类中没有定义静态方法。据说,当声明了静态数据成员但未定义时,也会发生LNK2019
此外,在尝试在子类内重新定义静态方法时,请小心: 不能重写子类中的静态方法,只能将其隐藏
和从C++标准:
9.4.1静态成员函数[class.Static.mfct] 2/A静态
成员函数不得为虚拟。不应有具有相同名称和相同参数类型的静态
和非静态成员函数(13.1)。静态
成员函数不得声明为常量
、易失性
、或常量易失性
:
#包括
福班
{
公众:
static void func(){std::您是否也在GContext
类中定义了静态方法?
#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;
}
A f
B f
A f
#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;
}