我试图从驱动程序类传递一个Integer对象作为我创建的SortedArray Generic类的函数的参数时遇到问题.从我的驱动程序类,我将用户的int输入转换为Integer对象,以转换为我的SortedArray类的Comparable.
我继续收到错误:"线程中的异常"主"java.lang.ClassCastException:java.lang.Integer无法强制转换为Comparable".我查看了一些同学的源代码,但发现参数/参数设置没什么差别,但是他们的代码工作得很好.我一直在寻找几个小时试图找到我所犯的错误,但我仍然无法找到为什么我的Integer对象无法转换为Comparable.
这里有一点来自我的SortedArray类
public class SortedArray implements Comparable{
public int size;
public int increment;
public int top;
Comparable[] a = new Comparable [size];
public SortedArray(int initialSize, int incrementAmount)
{
top = -1;
size = initialSize;
increment = incrementAmount;
}
public int appropriatePosition(Comparable value)
{
int hold = 0;
if(top == -1)
{
return 0;
}
else
{
for(int i = 0; i <= top; i++)
{
if(a[i].compareTo(value) > 0)
{
hold = i;
break;
}
}
}
return hold; …Run Code Online (Sandbox Code Playgroud) 为什么X中的方法测试不明确,如何解决?
struct A{};
struct B{};
template<typename T>
struct I { void test(T){} };
struct X : public I<A>, I<B> {};
int main(int argc, const char *argv[])
{
X x;
x.test(A());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
In function ‘int main(int, const char**)’:
error: request for member ‘test’ is ambiguous
error: candidates are: void I<T>::test(T) [with T = B]
error: void I<T>::test(T) [with T = A]
Run Code Online (Sandbox Code Playgroud) 以下代码无法编译,为什么会这样?我该如何解决这个问题?
struct A{
template<int N> int get() { return N; }
};
template <typename X>
struct B : public X {
template<int N> int get() {
return X::get<N>();
}
};
int main(int argc, const char *argv[])
{
B<A> b;
return b.get<5>();
}
Run Code Online (Sandbox Code Playgroud)
编译错误:
test.cxx: In member function ‘int B<X>::get()’:
test.cxx:8:30: error: expected primary-expression before ‘)’ token
test.cxx: In member function ‘int B<X>::get() [with int N = 5, X = A]’:
test.cxx:15:25: instantiated from here
test.cxx:8:30: error: invalid operands of types …Run Code Online (Sandbox Code Playgroud) 基本上,我很好奇你是否可以使用C++ 11模板来实现它,因此模板化函数可以检测迭代器的间接级别,并根据不同的方式编译函数.例如,以下是一些无法编译的代码:
#include <vector>
#include <list>
#include <type_traits>
#include <iostream>
struct MyStruct
{
int value;
MyStruct(int value = 42) : value(value) { }
const int& getInt() const { return value; }
};
typedef std::list<MyStruct> StructList;
typedef std::vector<const MyStruct*> StructPtrVector;
template <typename ITER_TYPE>
const int& getIteratorInt(ITER_TYPE iter)
{
if (std::is_pointer<decltype(*iter)>::value)
return (*iter)->getInt(); // Won't compile -> MyStruct has no operator-> defined
return iter->getInt(); // Won't compile -> MyStruct* has the wrong level of indirection
}
template <typename LIST_TYPE>
void PrintInts(const LIST_TYPE& …Run Code Online (Sandbox Code Playgroud) abstract public <T> T iterEdges(EdgeFun<T> func, T accum);
Run Code Online (Sandbox Code Playgroud)
这是针对图形的多线程库.我不是要求任何与实际实现相关的内容,我只是不明白双返回类型是什么意思?
我只是在猜这里,但这是我的解释(我试过谷歌搜索,但谷歌不匹配非字母数字符号,所以我尝试了几个术语的组合,但没有得到任何地方.)
它只是说它将返回某种T型集合?两个类扩展了此方法所在的类,因此我猜它允许多态,但这是什么意思呢?
我正在尝试编写一个通用函数来排序不同类型的数据.我的代码是:
#include<stdio.h>
#define GENERIC_SORT(TYPE) \
TYPE ##_SORT(TYPE a[],int n) \
{ \
int i,j; \
TYPE aux; \
for(i=1;i<n;i++) \
for(j=n-1;j>=i;j--) \
if(a[j]<a[j-1]) \
{ \
aux=a[j]; \
a[j]=a[j-1]; \
a[j-1]=aux; \
} \
}
GENERIC_SORT(int)
GENERIC_SORT(float)
GENERIC_SORT(double)
GENERIC_SORT(char)
int main(void)
{
int i,a[]={3,7,5,4,6,1};
int_SORT(a,6);
for(i=0;i<6;i++)
printf("%d ",a[i]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我正在准备考试,在课程中有一个例子GENERIC_MAX,它找到2个值之间的最大值.而且我应该像这样做...
它的工作原理罚款int,float,double和char.但是如何使用它来对字符串数组(char a[][100]或char *a[])进行排序?
我有一个功能
class A { <has copy and move constructors > };
void f(A &&a) { ... }
Run Code Online (Sandbox Code Playgroud)
我想用变量调用函数f x,但我想f在副本上运行x,而不是x自己运行.为什么我不能这样做?
f(copy(x));
Run Code Online (Sandbox Code Playgroud)
但相反必须这样做
f(A(x));
Run Code Online (Sandbox Code Playgroud)
有什么类似于copy我上面描述的功能吗?
注意:请不要告诉我添加一个f进行复制的重载,我想要一些不需要重载的东西并且是明确的(即向copy读者清楚说明复制是在这里完成的)
c++ generic-programming copy-constructor move-semantics c++11
我正在用C++实现一个通用堆栈(带有一个数组),并且对于在这种情况下返回什么感到困惑:
template <class T>
T Stack<T>::pop(void) {
if (size != 0) {
return items[size - 1];
size--;
} else {
cerr << "Cannot pop from empty stack." << endl;
return ???;
}
}
template <class T>
T Stack<T>::peek(void) {
if (size != 0)
return items[size - 1];
else {
cerr << "Cannot peek from empty stack." << endl;
return ???;
}
}
Run Code Online (Sandbox Code Playgroud)
我有什么选择?我认为做一些像声明一个新的T变量并返回它会很麻烦.我画了一个空白.
我想根据输入变量的类型有不同的变量值.码:
template <typename T>
int getValue(vector<T> & data)
{
return something; // There should be 0 for int and 1 for double
}
Run Code Online (Sandbox Code Playgroud)
有谁知道如何实现这样的功能?
处理某些类型需要使用成员/方法访问的事实的最佳方法是什么.操作员,而其他人使用 - >操作员.
编写代码是最好的.运算符并让调用者包装类型如下面的代码示例所示.
来自C#背景我不习惯这个特殊问题.
#include <iostream>
#include <string>
#include <vector>
#include <memory>
template<class T>
class container{
public:
void add(T element){
elements_.push_back(std::move(element));
}
void process(){
for(auto& a: elements_){
a.print();
}
}
private:
std::vector<T> elements_;
};
class printable{
public:
void print(){
std::cout << "Print\n";
}
};
template<class T>
class printable_forwarder{
public:
printable_forwarder(T element): element_{std::move(element)}{
}
void print(){
element_->print();
}
private:
T element_;
};
int main()
{
container<printable> c1;
c1.add(printable{});
c1.process();
container<printable_forwarder<std::shared_ptr<printable>>> c2;
std::shared_ptr<printable> sp{std::make_shared<printable>()};
c2.add(printable_forwarder<decltype(sp)>{sp});
c2.process();
}
Run Code Online (Sandbox Code Playgroud)
这看起来更好吗?
#include <iostream>
#include …Run Code Online (Sandbox Code Playgroud)