#ifndef __COMOPRE__
//----------------TComOpre.h
#define __COMOPRE__
class TComOpre
{
public:
HANDLE hd_com1,hd_com2; BOOL opencom(int port);
static CString getvalue();};#endif __COMOPRE__//----------------TComOpre.cpp
#include "stdafx.h"
#include "ComOpre.h"
BOOL TComOpre::opencom(int port)
{
return true;
}static CString getvalue()
{
return "xxx";
}
//----------------------
xxxx.cpp
#include "TComOpre.h"
....
void CMy008Dlg::Onopen1() 
{ CString aa;
aa = TComOpre.getvalue();
}
编译结果如下
------------------------------------------------------------------
--------------------Configuration: 008 - Win32 Debug--------------------
Compiling...
008Dlg.cpp
D:\XJ_Work_Station\test\1127\008\008Dlg.cpp(133) : error C2275: 'TComOpre' : illegal use of this type as an expression
        d:\xj_work_station\test\1127\008\comopre.h(5) : see declaration of 'TComOpre'
执行 cl.exe 时出错.008.exe - 1 error(s), 0 warning(s)

解决方案 »

  1.   

    static CString TComOpre::getvalue()
    {
    return "xxx";
    }
      

  2.   

    to vcmute(横秋) 不行
    --------------------Configuration: 008 - Win32 Debug--------------------
    Compiling...
    ComOpre.cpp
    D:\XJ_Work_Station\test\1127\008\ComOpre.cpp(11) : error C2724: 'getvalue' : 'static' should not be used on member functions defined at file scope
    执行 cl.exe 时出错.
    Creating browse info file...008.exe - 1 error(s), 0 warning(s)
      

  3.   

    CString::getvalue()
    {
    return "xxx";
    }
      

  4.   

    错了,是
    CString TComOpre::getvalue()
    {
    return "xxx";
    }
      

  5.   

    行了!!!
    to ringphone(临风)
    为什么在函数申明时要加static,但在具体函数的地方却不用加
      

  6.   

    cpp文件中的类静态函数要这样定义,结合你的程序就是
    CString TComOpre::getvalue()在调用时要这样
    void CMy008Dlg::Onopen1() 
    { CString aa;
    - aa = TComOpre.getvalue();
    + aa = TComOpre::getvalue();
    }