简体   繁体   English

类型为'double *'和'double'的无效操作数到二进制'operator +'

[英]invalid operands of types 'double*' and 'double' to binary 'operator+'

Here is my template class I am trying to implement: 这是我要实现的模板类:

#ifndef __ENCRYPTION_HEADER_INCLUDED__
#define __ENCRYPTION_HEADER_INCLUDED__

#include <iostream>  // in/out 
#include <fstream>   // file
#include <string>    // string
#include <sstream>   // string stream

using namespace std; // standard namespacing eliminates std:: from code


template <class mytemp>
class encryption 
{   
    public:

        mytemp * p;   

        encryption(); 

        mytemp encrypt_function (mytemp);
};


template <class mytemp>
encryption<mytemp>::encryption ()
{
    string line_in, file_name;
    mytemp input_value;

    cout << "input file name which contains encryption keys: " ;
    getline(cin, file_name);
    cout << '\n';   
    ifstream val_file (file_name.c_str());

    p = new mytemp[10];

    if (val_file.is_open())
    {
        while (getline(val_file, line_in))
        {
            istringstream line_out(line_in);
            line_out >> hex >> input_value;

            *p = input_value;
            p++;
        }

        p -= 10;
        val_file.close();
    }
    else
    {
        cout << "file not found" << '\n';
    }
}

template <class mytemp>
mytemp encryption<mytemp>::encrypt_function (mytemp input_en)
{
    mytemp output_en = input_en;

    for (mytemp k = 0; k < 10; k++)
    {
        output_en ^= *(p + k); // does not work with double or float but works with int?
    }
    return output_en;
}

#endif // __ENCRYPTION_HEADER_INCLUDED__ 

the line shown below is the one I am having a problem with: 下面显示的行是我遇到的问题:

output_en ^= *(p + k); // does not work with double or float but works with int? 

My understanding is that this is correct for an integer but it does not work for double or float and I am wondering if there is an easy way to get this to work other than rewriting the code to increment the pointer instead of incrementing through the pointed to values? 我的理解是,这对于整数是正确的,但不适用于double或float,并且我想知道是否存在一种简单的方法来使它工作,而不是重写代码以增加指针而不是通过指向指针进行递增值?

Pointer arithmetic expects a signed integer (ie, std::ptrdiff_t ) type, which double or float are neither. 指针算术期望带符号的整数 (即std::ptrdiff_t )类型,double和float都不是。 In this loop here: 在此循环中:

for (mytemp k = 0; k < 10; k++)
{
    output_en ^= *(p + k); // does not work with double or float but works with int?
}

It makes no sense to use a float or double. 使用浮点数或双精度数是没有意义的。 Just use a signed int (which int is). 只需使用一个带符号的int(即int )。

Bitwise xor isn't defined for those types. 未为这些类型定义按位异或。 If you're interpreting the inputs as just bits, then you should do what memcpy() or memset() do and just interpret them as an untyped array of bytes. 如果将输入仅解释为位,则应该执行memcpy()memset()操作,并将它们解释为无类型的字节数组。 That is, your template wrapper could determine the address and size of the data and pass those to a function that xors a block of memory. 也就是说,您的模板包装器可以确定数据的地址和大小,然后将其传递给对内存块进行异或处理的函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 类型为“ double”和“ const char [5]”的C ++无效操作数为二进制“ operator +” - C++ invalid operands of types 'double' and 'const char [5]' to binary 'operator+' int类型的无效操作数和二进制&#39;operator%&#39;的double - invalid operands of types int and double to binary 'operator%' 'double' 和 'int' 类型的无效操作数到二进制 'operator%' - invalid operands of types 'double' and 'int' to binary 'operator%' 二进制“ operator%”的类型为“ double”和“ int”的无效操作数 - invalid operands of types `double' and `int' to binary `operator%' 错误:“double”和“double”类型的无效操作数转换为二进制“operator%” - error: invalid operands of types ‘double’ and ‘double’ to binary ‘operator% 类型为“ double”的无效操作数snd const char [3]”转换为二进制“ operator &lt;&lt;” - invalid operands of types ‘double’ snd const char [3]’ to binary 'operator<<' 二进制“ operator!=” |的类型为&#39;double&#39;和&#39;const char [2]&#39;的无效操作数 - Invalid operands of types 'double' and 'const char [2]' to binary 'operator!='| 二进制“ operator +”类型为“ const char [8]”和“ const char *”类型的无效操作数 - Invalid operands of types ‘const char [8]’ and ‘const char*’ to binary ‘operator+ &#39;const char*&#39; 和 const char[4]&#39; 类型的无效操作数到二进制 &#39;operator+&#39; - invalid operands of types 'const char*' and const char[4]' to binary 'operator+' 类型指针和指向二进制运算符的类指针的操作数无效 - Invalid operands of types class pointer and class pointer to binary operator+
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM