主题:奇怪的LNK2005错误
今天编了个小程序,出现LNK2005错误,后来还是解决了,但还是感到很奇怪,我编的文件代码如下
//Array.h
#ifndef ARRAY_H
#define ARRAY_H
class Array
{
public:
Array();
int operator[](int index);
void addElement(int element);
virtual ~Array();
private:
int *a;
int capacity;
int used;
};
#endif
//Array.cpp
#include "Array.h"
Array::Array():capacity(50),used(0)
{
a=new int[capacity];
}
int Array::operator[](int index)
{
if(index>=0&&index<used)
return a[index];
}
void Array::addElement(int element)
{
if(used<capacity)
{
a[used]=element;
used++;
}
}
Array::~Array()
{
delete [] a;
}
//main.cpp
#include <iostream>
#include "Array.h"
#include "Array.cpp" //错在这里,把这句注释掉后错误解决了,奇怪!
using namespace std;
void main()
{
Array test1;
int next;
cin>>next;
while(next>=0)
{
test1.addElement(next);
cin>>next;
}
int i;
cin>>i;
cout<<test1[i];
//cout<<test1.a[i];
}
#include "Array.cpp"错在哪里呢,我还是看着书参考来编的,请高手指教。
//Array.h
#ifndef ARRAY_H
#define ARRAY_H
class Array
{
public:
Array();
int operator[](int index);
void addElement(int element);
virtual ~Array();
private:
int *a;
int capacity;
int used;
};
#endif
//Array.cpp
#include "Array.h"
Array::Array():capacity(50),used(0)
{
a=new int[capacity];
}
int Array::operator[](int index)
{
if(index>=0&&index<used)
return a[index];
}
void Array::addElement(int element)
{
if(used<capacity)
{
a[used]=element;
used++;
}
}
Array::~Array()
{
delete [] a;
}
//main.cpp
#include <iostream>
#include "Array.h"
#include "Array.cpp" //错在这里,把这句注释掉后错误解决了,奇怪!
using namespace std;
void main()
{
Array test1;
int next;
cin>>next;
while(next>=0)
{
test1.addElement(next);
cin>>next;
}
int i;
cin>>i;
cout<<test1[i];
//cout<<test1.a[i];
}
#include "Array.cpp"错在哪里呢,我还是看着书参考来编的,请高手指教。