[c++]templateError: no viable.. 求助啊啊啊

jeangq 2016-10-24 04:55:10
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/Array.cpp:100:31: No viable overloaded '='
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/Array.cpp:133:20: No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>'
/Users/jeangq/Documents/ccc/ccc/Section4.2b.2/Section4.2b.2/Array.cpp:118:24: No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>'
#ifndef Array_cpp
#define Array_cpp
//Implementation of Array class
#include "Array.hpp"
#include "Point.hpp"
#include "OutOfBoundsException.hpp"

namespace Corneliagao{
namespace CAD{


// Default Constructor
template <class T>
Array<T>::Array()
{
//Set the default size of array to 10
m_size = 10;
m_data = new Point[m_size];
}


//Constructor with a size argument
template <class T>
Array<T>::Array(int size)
{
//Store the user input to size
m_size = size;
m_data = new Point[m_size];
}

//copy constructor :obtain the size of C array and each elements data
template <class T>
Array<T>::Array(const Array & arr)
{
// Determine the size of my_arr
m_size = arr.m_size;

// Create an array by constructor
m_data = new Point[m_size];

for (int i = 0; i < m_size; i++)
{
// Assign each data by loop
m_data[i] = arr.m_data[i];
}
cout<<"The copy constructor is called"<<endl;
}

//Destructor
template <class T>
Array<T>::~Array()
{
// Delete Array

delete [] m_data;
}
//Initialize m_default_size as 5
template <class T>
int Array<T>::m_default_size =5;

//Reset the value of m_default_size
template <class T>
int Array<T>::DefaultSize(int size) const
{
m_default_size = size;
return m_default_size;
}

//Get the value of m_default_size
template <class T>
int Array<T>::DefaultSize() const
{
return m_default_size;
}

//output the size of the array
template <class T>
int Array<T>::Size()const
{
return m_size;
}

//will set an element of our array with a given point
template <class T>
void Array<T>::SetElement(const T& pt,int index)
{
if (index > m_size-1 || index < 0)
{
//OutOfBoundsException my_error(index);
throw OutOfBoundsException(index); }
else
{
m_data[index] = pt;//Error:No viable overloaded '='

}
}

//will get an element of our array with a given point
template <class T>
const Array<T>& Array<T>::GetElement(int index)
{
if (index > m_size-1 || index < 0)
{
//For handler with int type
throw OutOfBoundsException(index);//the index was too small or too big
}

else
{
// Otherwise, return the proper one
return m_data[index];//Error:No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>'
}
}

//operator is called for non-const objects.
template <class T>
const Array<T>& Array<T>::operator [] (int index)
{ if (index > m_size-1 || index < 0)
{
// If out of bound, return the first element
cout << "Index is out of bound,return the first element." << endl;
throw OutOfBoundsException(index);
}
else
{
return m_data[index];//Error:No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>'
}
}

// Assignment operator
template <class T>
Array<T>& Array<T>::operator = (const Array<T>& source)
{
cout << "Copy-Assignment Operator Called!\n" << endl;

if (this == &source)
{
cout << "The same Array " << endl;
return *this;
}
delete [] m_data;

cout <<"Deleted m_data array" << endl;

m_size = source.m_size; // shallow copy - this is not dynamic alloc

if (source.m_data) // if no zeros then there is a ref. - Deep copy needed
{
cout <<"im here"<<endl;

m_data = new Point[source.m_size]; // create a new pointee.

for (int i = 0; i < source.m_size; i++)
m_data[i] = source.m_data[i]; //copy the points to array
}
else
m_data = 0; //NULL

return *this;
}


// Operator to print an Array of objects, loop every object in the array and print it
template <class P>
ostream& operator << (ostream& os, const Array<P>& ar)
{
os << "Size of the Array = " << ar.m_size << endl;
for (int i = 0; i < ar.m_size; i++)
os << "Array [" << i << "]= "<< ar.m_data[i] << endl;
return os;
}

}
}
#endif


怎么调试都不对,实在不知道哪里出问题了,求助,感谢感谢~~
...全文
794 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
iyomumx 2016-10-24
  • 打赏
  • 举报
回复
引用 12 楼 jeangq 的回复:
[quote=引用 9 楼 iyomumx 的回复:] 一个 Array<T> 模板类,按字面上理解应该储存类型 T 的数据,但你的模板类储存的是 Point 类型的数据,而且整个模板几乎没有用到 T ,我认为应该是这里有问题。 另外,按道理 operator[] 和 GetElement 应该返回类型 T 而不是 Array<T>
谢谢,我把 Point* m_data;改成T* m_data; 然后其余Point 改成了T; operator[] 和 GetElement也修改了返回类型 T; 之后运行出现了这样一个错误,这是什么意思啊~~多谢多谢 "Corneliagao::CAD::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Corneliagao::CAD::NumericArray<int> const&)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)[/quote] http://bbs.csdn.net/topics/391996886#post-401444171
jeangq 2016-10-24
  • 打赏
  • 举报
回复
引用 11 楼 paschen 的回复:
[quote=引用 10 楼 jeangq 的回复:] [quote=引用 8 楼 paschen 的回复:] [quote=引用 6 楼 jeangq 的回复:] [quote=引用 4 楼 lx458004975 的回复:] 。。。。模板是不能.h和.cpp分开写的,放一个文件里吧
我把.cpp include在.hpp 里了呢 #ifndef Array_cpp #include "Array.cpp" #endif/* Array_cpp */[/quote] 模板不支持分离编译,把定义和实现放在同一个文件里 原因:http://blog.csdn.net/nestler/article/details/38731021 cpp文件不需要包含,只用添加到项目中[/quote] 谢谢,定义和实现如何放在一个文件里啊,放在.hpp 还是.cpp? 可以具体一点么,多谢~ 另外这个文章里说“如果在test.cpp中写一个函数,其中调用A<int>::f,则编译器会将其实例化出来,因为在这个点上(test.cpp中),编译器知道模板的定义,所以能够实例化,于是,test.obj的符号导出表中就有了A<int>::f这个符号的地址,于是连接器就能够完成任务。” “调用A<int>::f”指的是像在main.cpp里一样调用么,这个怎么操作啊~~~多谢多谢~~[/quote] 都可以,在同一个文件就行了 你引用的那句话是想说,如果在同一个编译单元里(模板实现部分与调用都在test.cpp中),那么是可以实例化出来,但如果不在同一个编译单元里(实现在test.cpp,调用在main中调用),那么就不行[/quote] 多谢多谢~~~
jeangq 2016-10-24
  • 打赏
  • 举报
回复
引用 9 楼 iyomumx 的回复:
一个 Array<T> 模板类,按字面上理解应该储存类型 T 的数据,但你的模板类储存的是 Point 类型的数据,而且整个模板几乎没有用到 T ,我认为应该是这里有问题。 另外,按道理 operator[] 和 GetElement 应该返回类型 T 而不是 Array<T>
谢谢,我把 Point* m_data;改成T* m_data; 然后其余Point 改成了T; operator[] 和 GetElement也修改了返回类型 T; 之后运行出现了这样一个错误,这是什么意思啊~~多谢多谢 "Corneliagao::CAD::operator<<(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, Corneliagao::CAD::NumericArray<int> const&)", referenced from: _main in main.o ld: symbol(s) not found for architecture x86_64 clang: error: linker command failed with exit code 1 (use -v to see invocation)
paschen 版主 2016-10-24
  • 打赏
  • 举报
回复
引用 10 楼 jeangq 的回复:
[quote=引用 8 楼 paschen 的回复:] [quote=引用 6 楼 jeangq 的回复:] [quote=引用 4 楼 lx458004975 的回复:] 。。。。模板是不能.h和.cpp分开写的,放一个文件里吧
我把.cpp include在.hpp 里了呢 #ifndef Array_cpp #include "Array.cpp" #endif/* Array_cpp */[/quote] 模板不支持分离编译,把定义和实现放在同一个文件里 原因:http://blog.csdn.net/nestler/article/details/38731021 cpp文件不需要包含,只用添加到项目中[/quote] 谢谢,定义和实现如何放在一个文件里啊,放在.hpp 还是.cpp? 可以具体一点么,多谢~ 另外这个文章里说“如果在test.cpp中写一个函数,其中调用A<int>::f,则编译器会将其实例化出来,因为在这个点上(test.cpp中),编译器知道模板的定义,所以能够实例化,于是,test.obj的符号导出表中就有了A<int>::f这个符号的地址,于是连接器就能够完成任务。” “调用A<int>::f”指的是像在main.cpp里一样调用么,这个怎么操作啊~~~多谢多谢~~[/quote] 都可以,在同一个文件就行了 你引用的那句话是想说,如果在同一个编译单元里(模板实现部分与调用都在test.cpp中),那么是可以实例化出来,但如果不在同一个编译单元里(实现在test.cpp,调用在main中调用),那么就不行
jeangq 2016-10-24
  • 打赏
  • 举报
回复
引用 8 楼 paschen 的回复:
[quote=引用 6 楼 jeangq 的回复:] [quote=引用 4 楼 lx458004975 的回复:] 。。。。模板是不能.h和.cpp分开写的,放一个文件里吧
我把.cpp include在.hpp 里了呢 #ifndef Array_cpp #include "Array.cpp" #endif/* Array_cpp */[/quote] 模板不支持分离编译,把定义和实现放在同一个文件里 原因:http://blog.csdn.net/nestler/article/details/38731021 cpp文件不需要包含,只用添加到项目中[/quote] 谢谢,定义和实现如何放在一个文件里啊,放在.hpp 还是.cpp? 可以具体一点么,多谢~ 另外这个文章里说“如果在test.cpp中写一个函数,其中调用A<int>::f,则编译器会将其实例化出来,因为在这个点上(test.cpp中),编译器知道模板的定义,所以能够实例化,于是,test.obj的符号导出表中就有了A<int>::f这个符号的地址,于是连接器就能够完成任务。” “调用A<int>::f”指的是像在main.cpp里一样调用么,这个怎么操作啊~~~多谢多谢~~
iyomumx 2016-10-24
  • 打赏
  • 举报
回复
一个 Array<T> 模板类,按字面上理解应该储存类型 T 的数据,但你的模板类储存的是 Point 类型的数据,而且整个模板几乎没有用到 T ,我认为应该是这里有问题。 另外,按道理 operator[] 和 GetElement 应该返回类型 T 而不是 Array<T>
paschen 版主 2016-10-24
  • 打赏
  • 举报
回复
引用 6 楼 jeangq 的回复:
[quote=引用 4 楼 lx458004975 的回复:] 。。。。模板是不能.h和.cpp分开写的,放一个文件里吧
我把.cpp include在.hpp 里了呢 #ifndef Array_cpp #include "Array.cpp" #endif/* Array_cpp */[/quote] 模板不支持分离编译,把定义和实现放在同一个文件里 原因:http://blog.csdn.net/nestler/article/details/38731021 cpp文件不需要包含,只用添加到项目中
jeangq 2016-10-24
  • 打赏
  • 举报
回复
引用 5 楼 fefe82 的回复:
No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>' 两种类型不能转换啊 ...
这个问题怎么调试啊,是哪里出了问题呢
jeangq 2016-10-24
  • 打赏
  • 举报
回复
引用 4 楼 lx458004975 的回复:
。。。。模板是不能.h和.cpp分开写的,放一个文件里吧
我把.cpp include在.hpp 里了呢 #ifndef Array_cpp #include "Array.cpp" #endif/* Array_cpp */
fefe82 2016-10-24
  • 打赏
  • 举报
回复
No viable conversion from 'Corneliagao::CAD::Point' to 'const Array<int>' 两种类型不能转换啊 ...
偏爱风流 2016-10-24
  • 打赏
  • 举报
回复
。。。。模板是不能.h和.cpp分开写的,放一个文件里吧
jeangq 2016-10-24
  • 打赏
  • 举报
回复
错误行号 93; 111;126 我有在后面标注了一下
jeangq 2016-10-24
  • 打赏
  • 举报
回复
//
//  Array.hpp
//  Section4.2b.2
//
//  Created by Cornelia on 10/16/16.
//  Copyright © 2016 corneliagao. All rights reserved.
//Defination of Array class

#ifndef Array_hpp
#define Array_hpp

#include <stdio.h>
#include "Point.hpp"
using namespace std;
namespace Corneliagao{
    namespace CAD{
        template <class T>
        class Array
        {
        private:
            int m_size;
            //Declare  m_default_size as ststic variable
            static int m_default_size;
            
        public:
            //dynamic C array of Point objects
            Point* m_data;
            
            //Default constructor
            Array();
            
            //Constructor with a size argument
            Array(int size);
            
            //copy constructor
            Array(const Array & arr);
            
            //Destructor
            ~Array();
            
            //Reset the value of m_default_size
            int DefaultSize(int size) const;
            
            
            //Get the value of m_default_size
            int DefaultSize() const;
            
            //output the size of the array
            int Size()const;
            
            //will set an element of our array with a given point
            void SetElement( const T& pt,int index) ;
            
            //will output an element of our array with a given point
            const Array<T>& GetElement(int index) ;
            
            // Assignment operator.
            Array<T>& operator = (const Array<T>& source);
            
            //operator is called for non-const objects.
            const Array<T>& operator [] (int index);
            
            
            
            template<typename P>
            // Operator to print an Array of objects
            friend ostream& operator << (ostream& os, const Array<P>& ar);
            
        };
    }
}
#ifndef Array_cpp
#include "Array.cpp"
#endif/* Array_cpp */
//When just include header, main.cpp can't see the template definition of Array::Array() 0r Array::~Array. When include Array.cpp, it could know current transformation of  Array<T>::Array() and Array<T>::~Array(). However, placing these code here is to put all of the template class code in the header file. Therefore, when include the header, all of the template code will be in one place, and main.cpp could identify them.
#endif /* Array_h */

//
//  Point.hpp
//  Section4.2b.2
//
//  Created by Cornelia on 10/16/16.
//  Copyright © 2016 corneliagao. All rights reserved.
//Defination of Point class
#ifndef Point_HPP
#define Point_HPP

#include <stdio.h>
#include <iostream>
using namespace std;
namespace Corneliagao{
    namespace CAD{
        
        class Point
        {
        private:
            // Data members
            double x;
            double y;
            
        public:
            // Constructors
            Point();                        // Default constructor
            Point(const Point &p);          //Copy constructor
            Point(double newx,double newy);// Initialize with x and y value
            
            // Destructor
            ~Point();
            
            // Accessing functions
            double X() const;// Get the x value
            double Y() const;// Get the y value
            
            void X(double newx);// Set the x value
            void Y(double newx);// Set the y value
            
            // Member operator overloading
            Point operator - () const;              // Negate the coordinates.
            Point operator * (double factor) const; // Scale the coordinates.
            Point operator + (const Point& p) const;// Add coordinates.
            bool operator == (const Point& p) const;// Equally compare operator.
            Point& operator = (const Point& source);// Assignment operator.
            Point& operator *= (double factor);     // Scale the coordinates & assign.
            
            double Distance()const; // Calculate the distance to the origin (0, 0).
            
            double Distance(const Point& p)const;// Calculate the distance between two points.
            
            // define toString()
            std::string ToString()const;
            
            // Send to ostream.Move inside the class definition and declare it as friend. The function remains a global function but it can now access the data members directly without the need for calling the getters or ToString() function.
            friend ostream& operator << (ostream& os, const Point& p);
            
        };
        
    }
}

#endif /* Point_hpp */
//
//  OutOfBoundsException.hpp
//  Section4.2b.2
//
//  Created by Cornelia on 10/16/16.
//  Copyright © 2016 corneliagao. All rights reserved.
#ifndef OutOfBoundsException_hpp
#define OutOfBoundsException_hpp

#include <sstream>
#include <iostream>
#include <stdio.h>
#include "ArrayException.hpp"
using namespace std;
namespace Corneliagao{
    namespace CAD{
        class OutOfBoundsException: public ArrayException
        {
        private:
            int m_index;
        public:
            //constructors
            //Defalt constructor
            OutOfBoundsException();
            
            //Initialize with m_index value
            OutOfBoundsException(int m_index);
            
            //Desturctor
            ~OutOfBoundsException();
            
            //overrriding function
            std::string GetMessage() const;
        };
        
        
        inline string OutOfBoundsException::GetMessage() const //Overrides ABC member funtion.
        {
            stringstream errMsg;
            errMsg << "Index " << m_index << " is out of bounds." << endl;
            return errMsg.str();
        }
    }
}
#endif /* OutOfBoundsException_hpp */
天外怪魔 2016-10-24
  • 打赏
  • 举报
回复
#include "Array.hpp" #include "Point.hpp" #include "OutOfBoundsException.hpp" 这3个的代码贴出来,还有,你现在贴的行号和错误提示中一致么?

65,204

社区成员

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

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