softyaさん、beatleさん、ありがとうございます。
pin_ptrについて教えていただきましたが・・・。
実はそれを使っても解決できないのではないか、いえ、まず使えないのではないかと思い始めてきました。
私が使用している統合開発環境はVisualC++2008です。これは純粋なC++を扱っているのですよね?
C++/CLIなんて聞いたことがないですし・・・。
そしてcli::pin_ptr<S>としてみましたがコンパイルは通りませんでした。
ところで、もしかして上記のコードでエラーが起こる原因は ref class を使っているからでしょうか。
だとするとpin_ptrを使用しても解決することはなさそうです。私は ref class は使用していないからです。聞いたこともありませんでしたしね・・・。
とりあえず詰んでいるコードを載せておきます。
次の3つの クラス,構造体 を宣言しています。
・平面ベクトルを扱うvplane構造体
・vectorを基底クラスとして<<演算子オーバーロードを加えたsvecクラス
・svec<vplane>を基底クラスとして比較演算子を加えたvpvecクラス
► スポイラーを表示
vplane.h
コード:
#ifndef _VPLANE_
#define _VPLANE_
struct vplane{
int x;
int y;
vplane(){}
vplane(int rx, int ry){
x = rx;
y = ry;
}
vplane operator+ (const vplane &input){
vplane vp;
vp.x = x+input.x;
vp.y = y+input.y;
return vp;
}
vplane operator- (const vplane &input){
vplane vp;
vp.x = x-input.x;
vp.y = y-input.y;
return vp;
}
vplane operator* (int num){
vplane vp;
vp.x = x*num;
vp.y = y*num;
return vp;
}
vplane operator/ (int num){
vplane vp;
vp.x = x/num;
vp.y = y/num;
return vp;
}
bool operator== (const vplane & target){
if(x!=target.x) return false;
if(y!=target.y) return false;
return true;
}
bool operator!= (const vplane & target){
return !(*this==target);
}
int vec(){
return (int)sqrt( (float)(x*x + y*y) );
}
};
vplane abs(const vplane vp){
return vplane(abs(vp.x), abs(vp.y) );
}
#endif
vpvec.h
コード:
#include "vplane.h"
#ifndef _VPVEC_
#define _VPVEC_
struct cls{};
template <typename x>
class svec :protected std::vector<x>{
public:
using std::vector<x>::operator[];
using std::vector<x>::size;
using std::vector<x>::resize;
using std::vector<x>::push_back;
using std::vector<x>::at;
svec(){}
svec & operator<< (cls input){
resize(0);
return *this;
}
svec & add(const x & input){
push_back(input);
return *this;
}
svec & operator<< (const x & input){
add(input);
return *this;
}
~svec(){}
};
class vpvec :protected svec<vplane>{
public:
using svec<vplane>::size;
using svec<vplane>::operator[];
using svec<vplane>::operator<<;
using svec<vplane>::at;
vpvec(){}
vpvec & add(int x, int y){
*this << vplane(x, y);
return *this;
}
bool operator== (const vpvec &target){
if(target.size() != size() ) return false;
for(size_t i=0;i<size();i++){
if(target[i] != at(i) ) return false;
}
return true;
}
bool operator!= (const vpvec &target){
return !(target == *this);
}
~vpvec(){}
};
#endif
エラー内容は下記の通りです。
コード:
------ ビルド開始: プロジェクト: HitJudging, 構成: Debug Win32 ------
コンパイルしています...
HitJudgingCPP.cpp
c:\documents and settings\administrator\my documents\visual studio 2008\projects\hitjudging\vpvec.h(58) : error C2678: 二項演算子 '!=' : 型 'const vplane' の左オペランドを扱う演算子が見つかりません (または変換できません) (新しい動作; ヘルプを参照)。
c:\program files\microsoft sdks\windows\v6.0a\include\guiddef.h(197): 'int operator !=(const GUID &,const GUID &)' の可能性があります。
c:\documents and settings\administrator\my documents\visual studio 2008\projects\hitjudging\vplane.h(50): または 'bool vplane::operator !=(const vplane &)'
引数リスト '(const vplane, vplane)' を一致させようとしているとき
ビルドログは "file://c:\Documents and Settings\Administrator\My Documents\Visual Studio 2008\Projects\HitJudging\Debug\BuildLog.htm" に保存されました。
HitJudging - エラー 1、警告 0
========== ビルド: 0 正常終了、1 失敗、0 更新不要、0 スキップ ==========