我尝试编写类似这样的类定义时遇到多个编译错误。
--auto-gen-config
我收到这样的错误..
trait BinOps[T] {
def +(that: Vector[T]): Vector[T]
def -(that: Vector[T]): Vector[T]
def *(that: Vector[T]): Vector[T]
def /(that: Vector[T]): Vector[T]
}
trait Vector[T] {
def toList(): List[T]
def zeros(length: Int): Vector[T]
}
object Vector {
def apply[T](args: T*): Vector[T] = new VectorImpl[T](args.toList)
private class VectorImpl[@specialized(Double, Int, Float, Long)T](val _data: List[T])
extends Vector[T]
with BinOps[T] {
def +(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 + elem._2))
def -(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 - elem._2))
def *(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 * elem._2))
def /(that: Vector[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 / elem._2))
def fill(length: Int, value: T): Vector[T] = new VectorImpl[T](List.fill[T](length)(value))
def toList(): List[T] = _data.toList
}
implicit def VectorToList[T](v: Vector[T]): List[T] = v.toList
}
然而,做一个zip应该导致一个类型为T的元组,我试图访问第一个和第二个元素。那么我做错了什么?
答案 0 :(得分:3)
错误:(37,95)value - 不是类型参数T
的成员
这是真正的错误(关于字符串的错误是#include<stdio.h>
#include<string.h>
#include<stdlib.h>
void main(void)
{
char str[] = "MyID-MyName-MyAddress-MyNumber--MyAlias";
char * str1, * token;
int i = 0;
int parsed_buffer_length = 0;
char * previous_delimiter_index = str;
char * delimiter_index = str;
for(;;)
{
str1 = str+parsed_buffer_length;
delimiter_index = strstr(str1,"-");
if(delimiter_index==NULL)
{
printf("%s",str1); //prints last token
break;
}
token = malloc(delimiter_index-previous_delimiter_index+1);
memset(token,'\0',delimiter_index-previous_delimiter_index+1));
strncpy(token,str1,(delimiter_index-previous_delimiter_index));
printf("%s\n",token);
parsed_buffer_length = (int)(parsed_buffer_length+delimiter_index-previous_delimiter_index+1);
previous_delimiter_index = delimiter_index+1;
free(token);
}
}
中隐式转换的不幸结果 - 我建议使用MyID
MyName
MyAddress
MyNumbers
MyAlias
构建以避免此类误导性错误。你的Predef
不受约束 - -Yno-predef
应该做什么?例如。如果T
是-
,那该怎么办?T
是什么?所以它不会编译。
答案 1 :(得分:0)
上面的所有答案都指向了正确的问题。我将发布最终有效的答案。正如JimN所建议的那样,以及来自scala irc的帮助,这最终对我有用。非常感谢您的解释
import Numeric.Implicits._
import Fractional.Implicits._
trait BinOps[T] {
def +(that: Vector[T])(implicit ev: Numeric[T]): Vector[T]
def -(that: Vector[T])(implicit ev: Numeric[T]): Vector[T]
def *(that: Vector[T])(implicit ev: Numeric[T]): Vector[T]
def /(that: Vector[T])(implicit ev: Fractional[T]): Vector[T]
}
trait Vector[T] {
def toList(): List[T]
def zeros(length: Int)(implicit ev: Numeric[T]): Vector[T]
}
object Vector {
def apply[T](args: T*): Vector[T] = new VectorImpl[T](args.toList)
private class VectorImpl[@specialized(Double, Int, Float, Long)T](val _data: List[T])
extends Vector[T]
with BinOps[T] {
def +(that: Vector[T])(implicit ev: Numeric[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 + elem._2))
def -(that: Vector[T])(implicit ev: Numeric[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 - elem._2))
def *(that: Vector[T])(implicit ev: Numeric[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 * elem._2))
def /(that: Vector[T])(implicit ev: Fractional[T]): Vector[T] = new VectorImpl[T](_data.zip(that).map(elem => elem._1 / elem._2))
def fill(length: Int, value: T): Vector[T] = new VectorImpl[T](List.fill[T](length)(value))
def zeros(length: Int)(implicit ev: Numeric[T]): Vector[T] = fill(length, ev.zero)
def toList(): List[T] = _data.toList
}
implicit def VectorToList[T](v: Vector[T]): List[T] = v.toList
}