小编use*_*001的帖子

operator << friend功能和模板

这是我的代码:

mov.h

#include <iostream>

template< class T>
class Movie {
public:
    Movie(T in) {
        a = in;
    }

    friend std::ostream& operator<<(std::ostream& os, const Movie<T>& movie);
private:
    T a;
};

template<class T>
std::ostream& operator<<(std::ostream& os, const Movie<T>& movie) {
    return os;
}
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include "mov.h"

int main() {
    Movie<int> movie1(1);

    std::cout << movie1 << std::endl;

    return 0;
}
Run Code Online (Sandbox Code Playgroud)

我尝试编译此代码,我得到错误:

Error 1 error LNK2019: unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class Movie<int> const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$Movie@H@@@Z) …
Run Code Online (Sandbox Code Playgroud)

c++ templates overloading operators friend

6
推荐指数
1
解决办法
121
查看次数

c ++中的回调函数

代码描述了两个实现回调函数的类,该函数必须是在模板中传递的类参数中的成员函数.在代码下面,我附上了相关的错误消息.

template <class CLASSNAME>
class a
{
public:
    typedef void (CLASSNAME::*myFunction)();

    a(CLASSNAME& myObject, myFunction callback) :
    m_myObject(myObject)
    {
        m_myFuntion = callback;
    }

    void update()
    {
        (m_myObject).*(m_myFuntion);
    }

    myFunction m_myFuntion;
    CLASSNAME& m_myObject;
};
Run Code Online (Sandbox Code Playgroud)

dummy.h

#include <stdio.h>

class dummy
{
public:
    dummy()
    {
        var = 14;
    }


    void func()
    {
        printf("func!!");
    }

    int var;
};
Run Code Online (Sandbox Code Playgroud)

main.cpp中

#include <cstdlib>
#include "a.h"
#include "dummy.h"


void main()
{
    dummy dum;

    a<dummy> avar(dum, &(dummy::func));

    avar.update();

    system("pause");
}
Run Code Online (Sandbox Code Playgroud)

我正在尝试实现回调函数,我收到以下错误消息:

C2298 missing call to bound pointer to …
Run Code Online (Sandbox Code Playgroud)

c++ function-pointers callback

2
推荐指数
1
解决办法
737
查看次数