首页 \ 问答 \ 指定一些模板参数(Specifying some template parameters)

指定一些模板参数(Specifying some template parameters)

我想编写一个自己的Vector类和一个计算交叉乘积的函数。

我的Vector-class有

template<class T, int dim, bool isRowVector>

T是条目的类型,dim是维度,isRowVector指定向量是行向量还是列向量。

我重载了一些Vector的运算符,特别是<<:

template <class T, int dim, bool isRowVec>
std::ostream& operator<<(std::ostream& os, Vector<T, dim, isRowVec>& vec) // <<
{
    os << "[" << vec[0];

    for(typename std::vector<T>::iterator it = vec.begin()+1; it != vec.end(); it++) {
        os << "," << *it;
    }

    os << "]";

    if(!isRowVec) { os << "^T"; }

    return os;
}

现在函数crossProduct应该只适用于dim = 3的Vectors。所以我指定了这样的函数:

template <class T, bool isRowVec>
Vector<T, 3, isRowVec> crossProduct (Vector<T, 3, isRowVec> a, Vector<T, 3, isRowVec> b) {
    T arr[3] = { a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0] };
    Vector<T, 3, isRowVec> newVec(arr);
    return newVec;
}

省略函数模板中的dim参数,因为它不必被猜测或声明。

这似乎不是一个问题,因为

Vector<float,3,true> a = crossProduct(f,g);

不会产生编译错误,其中f和g都是

Vector<float,3,true>

但是当我试着打电话的时候

std::cout << crossProduct(f, g) << std::endl;

我明白了

error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Vector<float, 3, true>')
  std::cout << crossProduct(f, g) << std::endl;
            ^

有任何想法吗?


I want to write an own Vector-class and a function which calculates the cross product.

My Vector-class has the

template<class T, int dim, bool isRowVector>

T is the type of the entries, dim the dimension and isRowVector specifies whether the vector is a row or a column vector.

I overloaded some operators of Vector, particularly <<:

template <class T, int dim, bool isRowVec>
std::ostream& operator<<(std::ostream& os, Vector<T, dim, isRowVec>& vec) // <<
{
    os << "[" << vec[0];

    for(typename std::vector<T>::iterator it = vec.begin()+1; it != vec.end(); it++) {
        os << "," << *it;
    }

    os << "]";

    if(!isRowVec) { os << "^T"; }

    return os;
}

Now the function crossProduct should only apply on Vectors with dim = 3. So I specified the function like this:

template <class T, bool isRowVec>
Vector<T, 3, isRowVec> crossProduct (Vector<T, 3, isRowVec> a, Vector<T, 3, isRowVec> b) {
    T arr[3] = { a[1]*b[2]-a[2]*b[1], a[2]*b[0]-a[0]*b[2], a[0]*b[1]-a[1]*b[0] };
    Vector<T, 3, isRowVec> newVec(arr);
    return newVec;
}

omitting the dim parameter in the function template, since it does not have to be guessed or stated.

This doesn't seem to be a problem, since

Vector<float,3,true> a = crossProduct(f,g);

does not create a compile-error, where f and g are both

Vector<float,3,true>

But when I try to call

std::cout << crossProduct(f, g) << std::endl;

I get

error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream<char>}' and 'Vector<float, 3, true>')
  std::cout << crossProduct(f, g) << std::endl;
            ^

Any ideas?


原文:https://stackoverflow.com/questions/27076490
更新时间:2022-07-09 15:07

最满意答案

由于Overtime ID在其名称中有一个空格,因此只需将括号[] OpenForm或在OpenForm调用中使用反引号。 您不能简单地使用下划线作为替换,因为它会更改列标识符。

DoCmd.OpenForm "frmEditOvertime",,, "[Overtime ID] = " & recordID, acFormEdit

DoCmd.OpenForm "frmEditOvertime",,, "`Overtime ID` = " & recordID, acFormEdit

并注意where参数可以使用任何兼容的SQL WHERE条件。


Because Overtime ID has a space in its name, simply bracket [] or use backticks in OpenForm call. You cannot simply use underscore as replacement as that changes the column identifier.

DoCmd.OpenForm "frmEditOvertime",,, "[Overtime ID] = " & recordID, acFormEdit

DoCmd.OpenForm "frmEditOvertime",,, "`Overtime ID` = " & recordID, acFormEdit

And do note the where argument can use any compliant SQL WHERE condition.

相关问答

更多
  • 您应该使用变量来验证验证结束时是否存在错误(或多个错误)。 下面的一些代码为您提供了一般的想法: $isError = false; if (!preg_match($namestring, $yourname)) { $error_message_name .= "Please enter a valid name"; $isError = true; } if (!preg_match($emailstring, $email_from)) { $error_messag ...
  • 因为 object value = 0; object value1 = 0; if (value == value1) // will be always false { } 当你比较两个类型object值然后equals运算符==比较它们的引用 - 两个对象是否引用相同的实例。 而是使用静态方法object.Equals ,在int情况下将使用value.Equals(value1) ,如果实例表示值类型将“解包”它们并比较它们的值(0 == 0) 。 If (Equals(value, valu ...
  • JavaScript关系运算符不能像您的代码所暗示的那样工作。 你必须做两个与&&单独比较 else if (200 > calorie_total && calorie_total > 100) 在这种特殊情况下,您并不需要这样做。 如果你把它变成了else ,那么这意味着“calorie_total”不能小于或等于100.事实上,如果“calorie_total”正好是100,这段代码就不会做任何事情! JavaScript relational operators don't work the wa ...
  • 两个问题: (1)而不是 if (sort=1) 你可能想要的 if (sort==1) 所有if statement都类似。 提醒, '='是赋值, '=='用于布尔比较。 (2) scanf ("%d", sort); ^ 需要& scanf ("%d", &sort); ^ 不幸的是,这两个都是常见的问题/错误。如果你提高编译器的警告级别,它可能会帮助你避免其中的一些。 Two problems: (1) Instead of if ( ...
  • 你正在比较指针值,这是不同的 你需要使用strcmp来比较c字符串。 或者使用std::string if (strcmp(val, file_data) == 0) { cout<<"Matched"<
  • 由于Overtime ID在其名称中有一个空格,因此只需将括号[] OpenForm或在OpenForm调用中使用反引号。 您不能简单地使用下划线作为替换,因为它会更改列标识符。 DoCmd.OpenForm "frmEditOvertime",,, "[Overtime ID] = " & recordID, acFormEdit DoCmd.OpenForm "frmEditOvertime",,, "`Overtime ID` = " & recordID, acFormEdit 并注意where ...
  • 所有if都相同。 后两者永远不会到达。 ... else if(textCol == cookie1 && backCol == cookie2) element.value = "black,yellow,black,black,black"; else if(textCol == cookie1 && backCol == cookie2) element.value = "black,#87CEFA,black,black,black"; ... All the if are sam ...
  • WriteLine和ReadKey是Shared方法,你没有一个实例可以和With一起With 您需要实例化一个变量才能在With使用它 WriteLine and ReadKey are Shared methods, you do not have an instance to use with the With You need to instantiate a variable in order to use it with With
  • 从我正在阅读的内容来看,您的程序可能会忽略您的一些说明。 试试这个问题解决方法: 1)将光标悬停在上方并单击代码。 If z = "Mils" Then 2)单击F9在该行上放置标记 3)逐步单击F8键(此键的作用是运行程序直到该代码。所以它基本上显示了程序的流程。) 4)检查F8是否跳过了以下任何代码: If z = "Mils" Then Set cx = Sheet8.range("D3", Sheet8.range("D3").End(xlDown)) For Each rng I ...
  • 将您的代码更改为: public class Grade { public static void main(String[] arguments) { int grade = 69; if (grade >= 90) { System.out.println("Well done you got a A"); } else if (grade >= 85) { System.out.println("Well done you got a B" ...

相关文章

更多

最新问答

更多
  • 获取MVC 4使用的DisplayMode后缀(Get the DisplayMode Suffix being used by MVC 4)
  • 如何通过引用返回对象?(How is returning an object by reference possible?)
  • 矩阵如何存储在内存中?(How are matrices stored in memory?)
  • 每个请求的Java新会话?(Java New Session For Each Request?)
  • css:浮动div中重叠的标题h1(css: overlapping headlines h1 in floated divs)
  • 无论图像如何,Caffe预测同一类(Caffe predicts same class regardless of image)
  • xcode语法颜色编码解释?(xcode syntax color coding explained?)
  • 在Access 2010 Runtime中使用Office 2000校对工具(Use Office 2000 proofing tools in Access 2010 Runtime)
  • 从单独的Web主机将图像传输到服务器上(Getting images onto server from separate web host)
  • 从旧版本复制文件并保留它们(旧/新版本)(Copy a file from old revision and keep both of them (old / new revision))
  • 西安哪有PLC可控制编程的培训
  • 在Entity Framework中选择基类(Select base class in Entity Framework)
  • 在Android中出现错误“数据集和渲染器应该不为null,并且应该具有相同数量的系列”(Error “Dataset and renderer should be not null and should have the same number of series” in Android)
  • 电脑二级VF有什么用
  • Datamapper Ruby如何添加Hook方法(Datamapper Ruby How to add Hook Method)
  • 金华英语角.
  • 手机软件如何制作
  • 用于Android webview中图像保存的上下文菜单(Context Menu for Image Saving in an Android webview)
  • 注意:未定义的偏移量:PHP(Notice: Undefined offset: PHP)
  • 如何读R中的大数据集[复制](How to read large dataset in R [duplicate])
  • Unity 5 Heighmap与地形宽度/地形长度的分辨率关系?(Unity 5 Heighmap Resolution relationship to terrain width / terrain length?)
  • 如何通知PipedOutputStream线程写入最后一个字节的PipedInputStream线程?(How to notify PipedInputStream thread that PipedOutputStream thread has written last byte?)
  • python的访问器方法有哪些
  • DeviceNetworkInformation:哪个是哪个?(DeviceNetworkInformation: Which is which?)
  • 在Ruby中对组合进行排序(Sorting a combination in Ruby)
  • 网站开发的流程?
  • 使用Zend Framework 2中的JOIN sql检索数据(Retrieve data using JOIN sql in Zend Framework 2)
  • 条带格式类型格式模式编号无法正常工作(Stripes format type format pattern number not working properly)
  • 透明度错误IE11(Transparency bug IE11)
  • linux的基本操作命令。。。