帮忙看看这个编译错误

aibo54 2007-03-26 05:48:50
Compiler Error C2724
'identifier' : 'static' should not be used on member functions defined at file scope

A static member function was defined at file scope. Static member functions should be declared with external linkage.

This is a warning when Microsoft extensions are used (/Ze) and is an error when Microsoft extensions are disabled (/Za).

The following is an example of this error:

class C
{
static void func();
};

static void C::func(){}; // error
...全文
337 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
aibo54 2007-03-30
  • 打赏
  • 举报
回复
终于明白了:
class C
{
static void func();
};

void C::func(){};

或直接
class C
{
static void func(){};
};
这样就对了!问题出在对static成员函数不够理解!
jjkezl 2007-03-26
  • 打赏
  • 举报
回复
class C
{
void func();
};

void C::func(){};
aibo54 2007-03-26
  • 打赏
  • 举报
回复
A static member function was defined at file scope. Static member functions should be declared with external linkage.

static member function 被定义在文件域,因该声明在.h文件里。
可能编译器认为:
static Savings::NoAccounts()
{
return count;
}
这的第一句是在声明,可能,可能.....感谢大家!
aibo54 2007-03-26
  • 打赏
  • 举报
回复
谢谢大家,可能是在.cpp文件中,去掉static
static Savings::NoAccounts()
{
return count;
}
变成
Savings::NoAccounts()
{
return count;
}
就不报错了。
aibo54 2007-03-26
  • 打赏
  • 举报
回复
干脆!
-----------------------------------------------------------------------------------
savings.h
-----------------------------------------------------------------------------------
#ifndef savings
#define savings
class Savings
{
public:
Savings(unsigned accNo,float balan = 0.0);
int AccountNo();
float AcntBalan();
static Savings * First();
Savings * Next();
static int NoAccounts();
void Display();
void Deposit(float amount);
void Withdrawal(float amount);
protected:
static Savings * pFirst;
Savings * pNext;
static int count;
unsigned acntNumber;
float balance;
};
#endif
-----------------------------------------------------------------------------------
saving.cpp
-----------------------------------------------------------------------------------
#include <iostream.h>
#include "savings.h"

Savings * Savings::pFirst = 0;
int Savings::count = 0;

Savings::Savings(unsigned accNo,float balan)
{
acntNumber = accNo;
balance = balan;

count++;
if(pFirst==0)
pFirst=this;
else
{
for(Savings * pS=pFirst;pS->pNext;pS=pS->pNext);
pS->pNext=this;
}
pNext=0;
}
int Savings::AccountNo()
{
return acntNumber;
}
float Savings::AcntBalan()
{
return balance;
}
static Savings * Savings::First()
{
return pFirst;
}
Savings * Savings::Next()
{
return pNext;
}
static Savings::NoAccounts()
{
return count;
}
void Savings::Display()
{
cout<<"Saving account number:"<<acntNumber<<"="<<balance<<endl;
}
void Savings::Deposit(float amount)
{
balance+=amount;
}

void Savings::Withdrawal(float amount)
{
if(balance<amount)
cout<<"Insufficient funds:balance"<<balance
<<",withdrawal"<<amount<<endl;
else
balance-=amount;
}

这是钱能的C++程序设计教程p364的一个例子。拜托了!
savior00 2007-03-26
  • 打赏
  • 举报
回复
去掉后面的static void C::func(){};
aibo54 2007-03-26
  • 打赏
  • 举报
回复
还是感谢yxq123(笨鸟先飞) ,savior00(VC不是这样用地!!)
aibo54 2007-03-26
  • 打赏
  • 举报
回复
an error when Microsoft extensions are disabled (/Za).
真不能懂这句话?
aibo54 2007-03-26
  • 打赏
  • 举报
回复
class C
{
static void func();
};

static void C::func(){};
他这个为啥有问题?
怎么解决他这个问题?
yxq123 2007-03-26
  • 打赏
  • 举报
回复
你最好看一看变量的特性,可能与这个有关。
我也不知道怎么解决。
aibo54 2007-03-26
  • 打赏
  • 举报
回复
没人帮助吗?我可是第一次发贴啊。呜呜
savior00 2007-03-26
  • 打赏
  • 举报
回复
是不是你静态函数声明错误了。

16,473

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧