【问题标题】:Unresolved external symbol "public:未解析的外部符号“public:
【发布时间】:2014-05-27 17:37:33
【问题描述】:

这是声明文件 MyImage.h:

#pragma once

template <class Pixel>
class MyImage
{
public:
    MyImage();
    MyImage(unsigned int w, unsigned int h);
    MyImage(const MyImage<Pixel>& copie);
    ~MyImage();
    MyImage<Pixel>& operator=(const MyImage<Pixel>& image); //quand on utilise  la variable MyImage, il faut écrire MyImage<Pixel>
    int getWidth() const;
    int getHeight() const;
    //resize(int w, int h);
    //T* operator[](int){return _bitmap+ i*_width;}  adresse du 1er element de la ligne

private:
    int _width;
    int _height;
    Pixel *_bitmap;

};

这是定义文件 MyImage.cpp:

#include "MyImage.h"

template <class Pixel>
MyImage<Pixel>::MyImage() : _width(0), _height(0), _bitmap(NULL)
{}

template <class Pixel>
MyImage<Pixel>::MyImage(unsigned int w, unsigned int h) : _width(w), _height(h)
{
    _bitmap = new Pixel[w*h];
}

template <class Pixel>
MyImage<Pixel>::MyImage(const MyImage<Pixel>& copie) : MyImage(_width, _height)
{
    for (int i = 0; i < _width*_height; i++)
    {
        _bitmap[i] = copie._bitmap[i];
    }
    // ou _bitmap = new bitmap(*(copie._bitmap));
}

template <class Pixel>
MyImage<Pixel>::~MyImage()
{
    delete _bitmap;
}

template <class Pixel>
MyImage<Pixel>& MyImage<Pixel>::operator=(const MyImage<Pixel>& image)
{
    if (this != &image)
    {
        _width = image._width;
        _height = image._height;
        delete _bitmap;
        for (int i = 0; i < _width*_height; i++)
        {
            _bitmap[i] = copie._bitmap[i];
        }
        // ou _bitmap = new bitmap(*(copie._bitmap));
    }
    return *this;
}

template <class Pixel>
int MyImage<Pixel>::getWidth() const
{
    return _width;
}
template <class Pixel>

int MyImage<Pixel>::getHeight() const
{
    return _height;
}

这是main函数main.cpp:

#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include "MyImage.h"

using namespace std;

int main()
{
    MyImage<unsigned char> image(20,20);    //  0-255
    //MyImage<unsigned char> image1(image);
    return 0;
}

我有一些错误,例如:

1>------ Build started: Project: TP5 Template Image, Configuration: Debug Win32 ------
1>  MyImage.cpp
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MyImage<unsigned char>::MyImage<unsigned char>(unsigned int,unsigned int)" (??0?$MyImage@E@@QAE@II@Z) referenced in function _main
1>main.obj : error LNK2019: unresolved external symbol "public: __thiscall MyImage<unsigned char>::~MyImage<unsigned char>(void)" (??1?$MyImage@E@@QAE@XZ) referenced in function _main
1>D:\TPCPP\TP5 Template Image\Debug\TP5 Template Image.exe : fatal error LNK1120: 2 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

在主函数中,我尝试声明“MyImage image();”没关系,但它不适用于“MyImage image(20,20);”

有人可以帮帮我吗?

【问题讨论】:

  • 模板的实现不能在 .cpp 文件中与标头分开,否则您会得到这些类型的链接器错误。这个站点上可能还有另一个问题,其中有更详细的答案来解释原因,但基本的解决方案是将实现移动到 .h 文件中。
  • 我将整个“MyImage.cpp”放入“MyImage.h”中,它可以工作。非常感谢

标签: c++ lnk2019 unresolved-external


【解决方案1】:

将类成员函数定义放在定义模板类的同一标题中。

【讨论】:

    猜你喜欢
    • 2021-04-18
    • 2018-11-13
    • 1970-01-01
    • 2012-12-19
    • 2011-02-09
    • 2015-06-27
    相关资源
    最近更新 更多