07-11-2011, 16:43
|
|
|
|
חבר מתאריך: 22.01.08
הודעות: 67
|
|
שגיאה בקוד
היי ,
בניתי תוכנית קטנה בסי++, בויג'ואל סטודיו 2010.
התוכנית היא מחלקה של וקטור . עם העמסת אופרטורים.
אני לא יודעת מה הבעיה בקוד שלי.
זאת השגיאה שקיבלתי:
קוד:
1>------ Build started: Project: templates, Configuration: Debug Win32 ------
1> Vector.cpp
1>Vector.obj : error LNK2001: unresolved external symbol "private: static int Vector<int>::size" (?size@?$Vector@H@@0HA)
1>C:\Users\USER\Documents\Visual Studio 2010\Projects\templates\Debug\templates.exe : fatal error LNK1120: 1 unresolved externals
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
אני מקווה שהעמסת אופרטורים שעשיתי טובה.
זה קובץ ההדר:
קוד:
#pragma once
#include <string>
using namespace std;
template <typename T>
class Vector
{
private:
T *data;
static int size;//number of elements in arr
int maxSize;//size of arr
public:
Vector(void){
this->maxSize=10;
this->size=0;
data=new T[maxSize];
}
Vector(int maxSize){
this->maxSize=maxSize;
this->size=0;
data=new T[maxSize];
}
Vector(const Vector& v){
if(v.size!=0){
this->maxSize=v.maxSize;
this->size=v.size;
data=new T[v.maxSize];
for (int i=0;i<v.maxSize;i++)
data[i]=v.data[i];
}
}
int MaxSize(){
return this->maxSize;
}
bool add(const T val) {
bool ans=false;
if (this->size < this->maxSize){
data[size]=val;
size=size+1;
ans=true;
}
return ans;
}
T remove(const T val) {
T ans=NULL;
for (int i=0;i<this->maxSize;i++){
if(data[i]==val){
data[i]=NULL;
size=size-1;
ans=data[i];
}
}
return ans;
}
void print(){
if( this->size!=0){
for (int i=0;i<size;i++)
cout<<data[i]<<",";
}
cout<<endl;
}
T at(const int i){
if (i<size)
return data[i];
else return NULL;
}
bool operator==(const Vector &f) const{
bool b=true;
if(f->size==this->size){
for(int i=0;i<size;i++)
if (f.data[i]!=this->data[i])
b=false;
}
else b=false;
return b;
}
Vector operator+(const Vector &f) {
for(int i=0;i<f.size;i++)
add(f.data[i]);
return *this;
}
~Vector(void){
delete data;
this->maxSize=0;
this->size=0;
cout<<"counstructor"<<endl;
}
};
וזה קובץ הCPP
קוד:
#include "Vector.h"
#include <iostream>
#include <string>
using namespace std;
int main(void){
Vector<int> *vec ;
vec=new Vector<int>();
for (int i=1; i<=vec->MaxSize(); i++) {
vec->add(i);
}
Vector<int> *vec1;
vec1= new Vector<int>(5);
for (int i=0; i<vec1->MaxSize(); i++) {
vec->add(i);
}
bool b=vec==vec1;
cout<<"vec==vec1?"<<b<<endl;
Vector<int> *vec2;
vec2=new Vector<int>();
vec2->operator+(*vec);
vec2->print();
system("pause");
return 0;
}
_____________________________________
|