C++ template vector 问题

tuu123fd 2012-03-09 03:37:28
MyVector.h

#ifndef _MYVECTOR_H_
#define _MYVECTOR_H_

#include <cmath>
#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

template <typename T>
class MyVector {
public:
MyVector ();
~MyVector ();
........
......
......
private:
std::vector<T> vPoint;
};

template <typename T>
inline MyVector<T> operator* (const T val, const MyVector<T>& myVector) {
return myVector * val;
}

template <typename T>
std::ostream& operator<<(std::ostream& stream, const MyVector<T>& myVector) {
stream << fixed;
stream << setprecision(2);
for (int i=0; i<myVector.getDimension(); i++) {
stream << myVector[i] << " ";
}
return stream;
}

#include "MyVector.cpp"

#endif


MyVector.cpp
#include "MyVector.h"

template <typename T>
MyVector<T>::MyVector(){

}

template <typename T>
MyVector<T>::~MyVector (){

}
........


Debug时问题
Multiple markers at this line
- redefinition of ‘MyVector<T>::MyVector()’
- ‘MyVector<T>::MyVector()’ previously declared here

大家指点一下......
...全文
327 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
乐哉悠哉 2012-03-09
  • 打赏
  • 举报
回复
你用的什么编译器?VC是不支持类模板分离编译的(即*.h和*.cpp不能在两个文件里),你可以直接在类定义里写函数实现试试。
pengzhixi 2012-03-09
  • 打赏
  • 举报
回复
都放头文件吧。
shiter 2012-03-09
  • 打赏
  • 举报
回复
为什么这些莫版看起来像乱码?
tuu123fd 2012-03-09
  • 打赏
  • 举报
回复
可以了 谢谢各位
大海啊全是水 2012-03-09
  • 打赏
  • 举报
回复
看你这个错误 应该是你的CPP 里面也需要
#ifndef MyVector_CPP
#define MyVector_CPP

//code

#endif
luz_com 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tuu123fd 的回复:]

在LINUX上用eclipse运在行的所以应该不那些问题
[/Quote]

你这句话到底是说,编译没有问题,还是想说试图编译但不行。

你应该把具体的编译环境告诉大家,到底是在什么平台上,用什么编译器编译的程序。

tuu123fd 2012-03-09
  • 打赏
  • 举报
回复
前提是不可修改MyVector.h
pengzhixi 2012-03-09
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tuu123fd 的回复:]
在LINUX上用eclipse运在行的所以应该不那些问题
[/Quote]
有没有问题你可以试试再说
tuu123fd 2012-03-09
  • 打赏
  • 举报
回复
在LINUX上用eclipse运在行的所以应该不那些问题

64,682

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧