C++ C+中的错误+;:使用模板重新定义类构造函数

C++ C+中的错误+;:使用模板重新定义类构造函数,c++,list,templates,C++,List,Templates,有人知道我如何修正这些错误吗? 我已经看了一段时间了,只是不知道该怎么办 错误: indexList.cpp:18: error: redefinition of `indexList<T>::indexList()' indexList.cpp:18: error: `indexList<T>::indexList()' previously declared here indexList.cpp:30: error: redefinition of `bool ind

有人知道我如何修正这些错误吗? 我已经看了一段时间了,只是不知道该怎么办

错误:

indexList.cpp:18: error: redefinition of `indexList<T>::indexList()'
indexList.cpp:18: error: `indexList<T>::indexList()' previously declared here
indexList.cpp:30: error: redefinition of `bool indexList<T>::append(T)'
indexList.cpp:30: error: `bool indexList<T>::append(T)' previously declared here
indexList.cpp:18:错误:重新定义'indexList::indexList()'
cpp:18:错误:`indexList::indexList()'以前在这里声明过
indexList.cpp:30:错误:重新定义'bool indexList::append(T)'
cpp:30:error:'bool indexList::append(T)'
cpp文件:

//
//
//
//
//
//
#include "indexList.h"
#include <iostream>
using namespace std;

//constuctor
//Descriptions: Initializes numberOfElement to 0
//              Initializes maxSize to 100
//Parameters:   none
//Return:       none
template <class T>
indexList<T>::indexList()
{
  numberOfElements = 0;
  maxSize = 100;
}


//Name: append
//Purpose: Adds element to the end of the list. If array is full, returns false
//Paramters: value - thing to append
//Return: true if append succeeds, false otherwise
template <class T>
bool indexList<T>::append(T value)
{
  if (maxSize > numberOfElements)
    {
      list[numberOfElements] = value;
      numberOfElements ++;
      return true;
    }
  else
    return false;
}
//
//
//
//
//
//
#包括“indexList.h”
#包括
使用名称空间std;
//建设者
//描述:将numberOfElement初始化为0
//将maxSize初始化为100
//参数:无
//返回:无
模板
indexList::indexList()
{
numberOfElements=0;
最大尺寸=100;
}
//名称:追加
//目的:将元素添加到列表的末尾。如果数组已满,则返回false
//参数:要附加的值
//Return:如果append成功,则为true,否则为false
模板
布尔索引列表::追加(T值)
{
如果(maxSize>numberOfElements)
{
列表[numberOfElements]=值;
numberOfElements++;
返回true;
}
其他的
返回false;
}
我没有把所有的错误都放在cpp文件中,因为其余的错误与上面的错误相似,而且很长

标题:

#include <iostream>
using namespace std;


#ifndef INDEXLIST_H
#define INDEXLIST_H

template <class T>
class indexList
{
 public:
 indexList();

 bool append(T value);

 bool insert(int indx, T value);

 bool replace(int indx, T newValue);

 bool retrieve(int indx, T &value) const;

 bool remove(int indx);

 void sort();

 int search(T value) const;

private:
 T list[100];
 int numberOfElements;
 int maxSize;
};


template <class T>
ostream &operator<<(ostream &outStream, const indexList<T> &lst);

#include "indexList.cpp"

#endif
#包括
使用名称空间std;
#ifndef索引列表\u H
#定义索引列表
模板
类索引列表
{
公众:
indexList();
bool附加(T值);
布尔插入(int indx,T值);
布尔替换(int indx,T newValue);
布尔检索(int indx、T和value)常量;
bool-remove(int-indx);
无效排序();
int搜索(T值)常量;
私人:
T表[100];
整数元;
int-maxSize;
};
模板

ostream&operator两个文件中的每一个都试图包含另一个文件,这可能会导致预处理器输出一些重复的代码

从文件
indexList.cpp
中取出
#include“indexList.h”

此外,构建过程不应尝试将
indexList.cpp
编译到对象文件中


另一种安排方式是将当前所有的内容放在
indexList.cpp
中靠近
indexList.h
的末尾,这样就根本没有
indexList.cpp
文件了。

两个文件都试图包含另一个,这可能会导致预处理器输出一些重复的代码

从文件
indexList.cpp
中取出
#include“indexList.h”

此外,构建过程不应尝试将
indexList.cpp
编译到对象文件中


另一种安排方式是将当前所有的内容放在
indexList.cpp
indexList.h
末尾附近,这样根本就不会有
indexList.cpp
文件。

将任何模板化方法放在
indexList.inl
文件中,并将其包含在标题中。不要包含此文件的标题


将任何其他方法放入
indexList.cpp
文件中。包含此文件的标题。

将任何模板化方法放入
索引列表.inl
文件中,并包含标题中的方法。不要包含此文件的标题


将任何其他方法放入
indexList.cpp
文件中。包含此文件的标题。

您所做的一切都是可以的,但您必须意识到.cpp文件本身不应编译为对象,而应包含应用程序代码中的.h文件:

// main.cc

#include "indexList.h"

int main()
{
    indexList<int> il;
}

c++ -o main main.cc
//main.cc
#包括“indexList.h”
int main()
{
指数主义者;
}
C++主控
我敢打赌,这是因为您尝试执行了
c++-c indexList.cpp
c++indexList.cpp
,所以会出现错误(或者您的make工具正在为源代码目录中的所有.cpp文件自动尝试此操作-您可以尝试将indexList.cpp重命名为indexList.inl或.inc或其他名称-记住也更改indexList.h中的名称-以查看是否解决了问题),在这种情况下,定义会出现两次:一次是编译包含indexList.h,另一次是编译完成indexList.cpp


无论如何,没有必要在.cpp中包含indexList.h-这使indexList.cpp看起来像是为单独编译而设计的。

您所做的一切都是可以的,但您必须意识到.cpp文件本身不应该编译成对象-相反,只需从应用程序代码中包含.h文件即可:

// main.cc

#include "indexList.h"

int main()
{
    indexList<int> il;
}

c++ -o main main.cc
//main.cc
#包括“indexList.h”
int main()
{
指数主义者;
}
C++主控
我敢打赌,这是因为您尝试执行了
c++-c indexList.cpp
c++indexList.cpp
,所以会出现错误(或者您的make工具正在为源代码目录中的所有.cpp文件自动尝试此操作-您可以尝试将indexList.cpp重命名为indexList.inl或.inc或其他名称-记住也更改indexList.h中的名称-以查看是否解决了问题),在这种情况下,定义会出现两次:一次是编译包含indexList.h,另一次是编译完成indexList.cpp


无论如何,没有必要在.cpp中包含indexList.h,这使得indexList.cpp看起来像是为单独编译而设计的。

除了
indexList.h
之外,还做任何事情来包含文件
indexList.cpp
?有一个indexList.cpp,但它只有一个#include“indexList.h”您需要
包含吗“indexList.h“
在indexList.cpp中?indexList.cpp第18行和第30行是哪些行?我试图数数,但无法匹配这些行…我添加了注释,以弥补我忽略的部分,因为除了
indexList.h
包含文件
indexList.cpp
?有一个indexList.cpp,但它只有一个#include”“indexList.h”是否需要
#包括“indexList.h”