Friend.h で
#ifndef FRIEND
#define FRIEND
class Friend
{
public:
static int i ;
int j;
Friend(void);
~Friend(void);
}frnd1;
#endif
Friend.cpp で
#include "Friend.h"
int Friend::i = 9;
extern Friend frnd1;
Friend::Friend(void)
{
}
Friend::~Friend(void)
{
}
main.cpp で
#include <iostream>
using namespace std;
#include"Friend.h"
int main()
{
frnd1.j = 9;
cout<<"hello";
getchar();
return 0;
}
上記のコードを実行すると、次のリンカ エラーが発生します。
error LNK2005: "class Friend frnd1" (?frnd1@@3VFriend@@A) already defined in main.obj
メイン関数でグローバル オブジェクトを使用する方法を理解できません。