在.cpp文件中的模板类(struct)中编写非模板方法的实现是否可行?我已经读过模板方法应该写在.h上,但我的方法不是模板方法,尽管它属于模板类.这是我的.h中的代码:
#include <iostream>
#ifndef KEY_VALUE_H
#define KEY_VALUE_H
using namespace std;
namespace types
{
template <class T, class U>
struct key_value
{
T key;
U value;
static key_value<T, U> make(T key, U value)
{
key_value<T, U> kv;
kv.key = key;
kv.value = value;
return kv;
};
string serialize()
{
// Code to serialize here I want to write in .cpp but fails.
}
};
}
#endif /* KEY_VALUE_H */
Run Code Online (Sandbox Code Playgroud)
我试着serialize()在.cpp文件中编写方法的实现,如下所示:
#include "key_value.h"
using namespace types;
template <class T, class …Run Code Online (Sandbox Code Playgroud) 我尝试使用 C++ 实现一个简单的 HTTP 服务器。我能够向浏览器发送文本响应,但无法发送二进制文件请求的响应。
这是我的代码,用于获取 PNG 文件请求的 HTML 响应:
string create_html_output_for_binary(const string &full_path)
{
const char* file_name = full_path.c_str();
FILE* file_stream = fopen(file_name, "rb");
string file_str;
size_t file_size;
if(file_stream != nullptr)
{
fseek(file_stream, 0, SEEK_END);
long file_length = ftell(file_stream);
rewind(file_stream);
// Allocate memory. Info: http://www.cplusplus.com/reference/cstdio/fread/?kw=fread
char* buffer = (char*) malloc(sizeof(char) * file_length);
if(buffer != nullptr)
{
file_size = fread(buffer, 1, file_length, file_stream);
stringstream out;
for(int i = 0; i < file_size; i++)
{
out << buffer[i];
}
string copy …Run Code Online (Sandbox Code Playgroud) 我使用头文件(.h)和定义(.cpp)文件在C++中创建了一个简单的类'Hello'.这是头文件内容:
#ifndef HELLO_H
#define HELLO_H
#include <string>
namespace test
{
class Hello
{
private:
std::string name;
public:
Hello();
void say_hello();
};
}
#endif
Run Code Online (Sandbox Code Playgroud)
定义文件内容正如您所期望的那样:
#include "Hello.h"
#include <iostream.h>
using namespace test;
Hello::Hello()
{
this->name = "Yoppy Yunhasnawa";
}
void Hello::say_hello()
{
string message = "Hello, " + this->name + ".. Have nice day!";
cout << message << "\n";
}
Run Code Online (Sandbox Code Playgroud)
我把这个类包含在main.cpp文件中并像这样使用它:
#include "Hello.h"
using namespace test;
int main(int argc, char *argv[])
{
Hello* hello = new Hello;
hello->say_hello();
}
Run Code Online (Sandbox Code Playgroud)
当我用这样的g ++编译main.cpp文件时, …
我有一个我定义的基类,如下所示:
namespace yxs
{
class File
{
public:
File(std::string fileName);
virtual ~File();
bool isExists();
size_t size();
protected:
std::string fileName;
std::ifstream* inputStream;
std::ofstream* outputStream;
}
Run Code Online (Sandbox Code Playgroud)
然后我创建了一个继承上述基类的子类:
namespace yxs
{
class InputFile : File
{
public:
InputFile(std::string fileName);
virtual ~InputFile();
};
}
Run Code Online (Sandbox Code Playgroud)
在另一个不相关的类中,我实例化了子类并尝试调用该方法: isExists()
void yxs::Engine::checkFile()
{
bool isOK = this->inputFile->isExists(); // Error on compile in this line
if(!isOK)
{
printf("ERROR! File canot be opened! Please check whether the file exists or not.\n");
exit(1);
}
}
Run Code Online (Sandbox Code Playgroud)
但是,该应用程序将无法编译.编译器给出了两条错误消息:
Engine.cpp:66:34: 'isExists' is a private …
我使用ZF2表单制作了典型表单,并希望使用ZF2 InputFilter添加验证.这是成功但错误信息的颜色是黑色,看起来很奇怪.我尝试使用我搜索过的方法更改颜色,如下所示:
array(
'name' =>'NotEmpty',
'options' => array(
'messages' => array(
NotEmpty::IS_EMPTY => '<div style="color:red;">Please enter User Name!</div>'
),
),
),
Run Code Online (Sandbox Code Playgroud)
但是,它不是将消息的颜色更改为红色,而是显示带有样式的标记,换句话说,只显示纯HTML.达到我需要的正确方法是什么?