2013-03-26 61 views
0

我有一組SplinePoints和InterpolatedPoints。他們的聯盟必須存儲在FinalInterpolatedPoints中。C++中兩組結構的聯合

這是主要的文件:

#include <iostream> 
#include <vector> 
#include <conio.h> 
#include <cmath> 
#include <algorithm> 
#include <iterator> 
#include <set> 

using namespace std; 

typedef struct SplinePoints { 
int x; 
double y; 
SplinePoints(int a, double b) : x(a), y(b) { 

} 
friend bool operator < (SplinePoints const&A, SplinePoints const&B) { 
    return A.x < B.x; 
} 
}; 

typedef struct InterpolatedPoints { 
int x; 
double y; 
InterpolatedPoints(int a, double b) : x(a), y(b) { 

} 
friend bool operator < (InterpolatedPoints const&A, 
       InterpolatedPoints const&B) { 
    return A.x < B.x; 
} 
}; 

typedef struct FinalInterpolatedPoints { 
int x; 
double y; 
FinalInterpolatedPoints(int a, double b) : x(a), y(b) { 

} 
friend bool operator < (FinalInterpolatedPoints const&A, 
       FinalInterpolatedPoints const&B) { 
    return A.x < B.x; 
} 
FinalInterpolatedPoints operator= (SplinePoints const&A) { 
    x = A.x; 
    y = A.y; 
    return *this; 
} 
FinalInterpolatedPoints operator= (InterpolatedPoints const&A) { 
    x = A.x; 
    y = A.y; 
    return *this; 
} 
}; 

inline bool operator < (InterpolatedPoints const&A, 
       SplinePoints const&B) { 
return A.x < B.x; 
} 

int main (int argc, char** argv) { 

set <SplinePoints> set1; 
set <InterpolatedPoints> set2; 
set <FinalInterpolatedPoints> BaseLine; 

set1.insert(SplinePoints(1,2)); 
set1.insert(SplinePoints(2,5)); 
set1.insert(SplinePoints(3,8)); 
set1.insert(SplinePoints(4,1.66)); 

set2.insert(InterpolatedPoints(5,5.768)); 
set2.insert(InterpolatedPoints(6,5.560)); 
set2.insert(InterpolatedPoints(7,5.643)); 
set2.insert(InterpolatedPoints(8,5.313)); 

set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(BaseLine, BaseLine.begin())); 

getch(); 
return 0; 
} 

我用這個功能來做到這一點:

set_union(set1.begin(), set1.end(), set2.begin(), set2.end(), inserter(BaseLine, BaseLine.begin())); 

,其中設置1,設置2和基線分別爲類型SplinePoints,InterpolatedPoints和FinalInterpolatedPoints的。

當我調試程序我得到模板超載錯誤<和「=」運算符是參照alogrithm

template<class _InIt1, 
class _InIt2, 
class _OutIt> inline 
_OutIt _Set_union(_InIt1 _First1, _InIt1 _Last1, _InIt2 _First2, _InIt2 _Last2, _OutIt _Dest) 
{ // OR sets [_First1, _Last1) and [_First2, _Last2), using operator< 
for (; _First1 != _Last1 && _First2 != _Last2;) 
    **if (_DEBUG_LT(*_First1, *_First2))** 
     { // copy first 
     ***_Dest++ = *_First1;** 
     ++_First1; 
     } 
    **else if (*_First2 < *_First1)** 
     { // copy second 
     ***_Dest++ = *_First2;** 
     ++_First2; 
     } 
    else 
     { // advance both 
     ***_Dest++ = *_First1;** 
     ++_First1; 
     ++_First2; 
     } 
_Dest = _STD copy(_First1, _Last1, _Dest); 
return (_STD copy(_First2, _Last2, _Dest)); 
} 

我包括運營商的結構定義重載函數,但我是頭文件的源文件能夠擺脫僅與<功能有關的錯誤。 我仍然有在消除有關=即錯誤:

error C2679: binary '=' : no operator found which takes a right-hand operand of type 'const SplinePoints' (or there is no acceptable conversion)

請幫助困難!

+0

檢查此問題http://stackoverflow.com/questions/4331962/and-operator-overloading-problem – 999k 2013-03-26 13:39:42

回答

0

嘗試:

FinalInterpolatedPoints operator= (const SplinePoints &A) 

即,類型(SplinePoints)之前移動至const關鍵字。

0

有些事情與您的代碼有些奇怪。降typedef在班前:

/*drop: typedef*/ struct SplinePoints { 

而且,你operator= S的關係返回一個引用,例如,

FinalInterpolatedPoints& operator= (SplinePoints const&A) { 
//     ^add this! 
    x = A.x; 
    y = A.y; 
    return *this; 
} 

除此之外,你還沒有提供編譯和完整的例子。如果修復上述問題後仍然有問題,請創建一個並編輯該問題。

+0

這將無法編譯。當你不使用typedef時,SpliPoints必須被聲明爲一個結構體。在函數定義的錯誤位置有一個const。 – fredrik 2013-03-26 14:11:56

+0

@fredrik:在C中可能會出現這種情況(即使這對我來說看起來很奇怪),但肯定不在C++中。這個問題明確標記爲C++。 – 2013-03-26 14:14:29

+0

@DanielFrey我刪除了typedef並按照你所說的方式更改了代碼,但仍然收到相同的錯誤。 – 2013-03-26 14:32:36