软件注册站
热情软件屋

 
LNK2001: unresolved external symbol "private: static int CData::B" (?B@CData@@0HA)
编号: QA004910    
建立日期: 2005年1月18日 最后修改日期: 2005年1月18日
所属类别: C/C++ - 错误信息
   
    我编了一个小程序,想演示一下对于static成员直接使用类调用,结果出错:
    Generating Code...
    Linking...
    Data.obj : error LNK2001: unresolved external symbol "private: static int CData::B" (?B@CData@@0HA)
    Data.obj : error LNK2001: unresolved external symbol "private: static int CData::A" (?A@CData@@0HA)
    Data.obj : error LNK2001: unresolved external symbol "private: static int CData::C" (?C@CData@@0HA)
    Release/test2.exe : fatal error LNK1120: 3 unresolved externals
    Error executing link.exe.
    
    test2.exe - 4 error(s), 0 warning(s)
    代码如下:
    // Data.h: interface for the CData class.
    
//
    
//////////////////////////////////////////////////////////////////////
    
    
#if !defined DATA_H
    
#define DATA_H
    
    
#if _MSC_VER > 1000
    
#pragma once
    
#endif // _MSC_VER > 1000
    
    
class CData
    
{
    
public:
    
    
static void Print();
    
static void Add();
    
static void Set(int i, int j);
    
    
CData();
    
virtual ~CData();
    
    
private:
    
static int A;
    
static int B;
    
static int C;
    
    
};
    
    
#endif // !defined(AFX_DATA_H__330778C5_0D94_4045_B715_60ABDDB71B7F__INCLUDED_)
    
    
// Data.cpp: implementation of the CData class.
    
//
    
//////////////////////////////////////////////////////////////////////
    
    
#include "stdafx.h"
    
#include "Data.h"
    
    
#include "stdio.h"
    
    
//////////////////////////////////////////////////////////////////////
    
// Construction/Destruction
    
//////////////////////////////////////////////////////////////////////
    
    
CData::CData()
    
{
    
    
}
    
    
CData::~CData()
    
{
    
    
}
    
    
void CData::Set(int i, int j)
    
{
    
A = i;
    
B = j;
    
    
}
    
    
    
void CData::Add()
    
{
    
C = A + B;
    
    
}
    
    
void CData::Print()
    
{
    
printf("%d\n", C);
    
    
}
    
    
// test2.cpp : Defines the entry point for the console application.
    
//
    
    
#include "stdafx.h"
    
    
#include "data.h"
    
    
int main(int argc, char* argv[])
    
{
    
// test2.cpp : Defines the entry point for the console application.
    
//
    
    
#include "stdafx.h"
    
    
#include "data.h"
    
    
int main(int argc, char* argv[])
    
{
    
    
CData::Set(4, 5);
    
CData::Add();
    
CData::Print();
    
    
return 0;
    
    
}// test2.cpp : Defines the entry point for the console application.
    
//
    
    
#include "stdafx.h"
    
    
#include "data.h"
    
    
int main(int argc, char* argv[])
    
{
    
    
CData::Set(4, 5);
    
CData::Add();
    
CData::Print();
    
    
return 0;
    
    
}// test2.cpp : Defines the entry point for the console application.
    
//
    
    
#include "stdafx.h"
    
    
#include "data.h"
    
    
int main(int argc, char* argv[])
    
{
    
    
CData::Set(4, 5);
    
CData::Add();
    
CData::Print();
    
    
return 0;
    
    
}
    
(佚名)
   
    在Data.h中定义的静态变量必须在Data.cpp中进行初始化,即使成员定义的是私有的。在Data.cpp中添加:
    int CData::A = 0;
    
int CData::B = 0;
    
int CData::C = 0;
    

    

此问题由李海回答。

附加关键字:编程, 源程序, programming, source code, C/C++, MFC, C++ Builder, Borland C++, Turbo C, C, BCB, 错误信息, error, error message, link, compile, runtime

   
 
把这个问题推荐给朋友
   
 
   
您的意见类别
您的名字
您的电子邮件
您的建议(请尽可能详细)
 
 

版权所有 1997-2008 热情软件屋
如果您有任何建议和意见, 请给我发个电子邮件 askpro@china-askpro.com
Web Designed by ZebraStudio