gpt4 book ai didi

c++ - 编译的 C++ 程序充满了 mojibake?

转载 作者:塔克拉玛干 更新时间:2023-11-03 08:00:08 25 4
gpt4 key购买 nike

#include "StdAfx.h"
#include <stdlib.h>
#include <iostream>

#include "mysql_connection.h"

#include <cppconn/driver.h>
#include <cppconn/exception.h>
#include <cppconn/resultset.h>
#include <cppconn/statement.h>
#include <cppconn/prepared_statement.h>

using namespace std;

int main(void){
cout << endl;

try{
sql::Driver *driver;
sql::Connection *con;
sql::Statement *stmt;
sql::ResultSet *res;
sql::PreparedStatement *pstmt;

driver = get_driver_instance();
con = driver->connect("REMOVED", "REMOVED", "REMOVED");
con->setSchema("REMOVED");

stmt = con->createStatement();
res = stmt->executeQuery("SELECT username FROM player WHERE id=1");

cout << "Username: " << res->getString("username") << endl;

delete res;
delete con;

cout << "Done.";
system("pause");

}catch(sql::SQLException &e){
cout << "# ERR: SQLException in " << __FILE__;
cout << "(" << __FUNCTION__ << ") on line " << __LINE__ << endl;
cout << "# ERR: " << e.what();
cout << " (MySQL error code: " << e.getErrorCode() << endl;
cout << ", SQLState: " << e.getSQLState() << " )" << endl;
system("pause");
}

return 0;
}

变成... http://i.imgur.com/cIVnl.png

这是怎么回事? :( 这只是我使用 C++ 的第二天,所以请原谅我糟糕的格式化编码和其他笨拙的错误。这总是在我遇到未处理的异常之前出现。

最佳答案

我相信您正在使用的 API 会返回 std::string 对象。

在这种情况下,您可能想要添加一个 #include <sstream>到代码的顶部,以便为 std::string 类定义一个“<<”运算符。

我不确定这个解决方案,因为我希望你会得到一个编译错误,以某种方式暗示找不到运算符“<<”。

尝试将其粘贴到您的代码中并立即从 main() 中调用它,以查看流操作符是否正常工作。请注意,这将使您的程序退出。

void SimpleStrings()
{
std::string str("String in a std::string");
const char *psz = "const-char-str";
std::cout << "std::string: " << str << std::endl << "const char *: " << psz << std::endl;
exit(1);
}

关于c++ - 编译的 C++ 程序充满了 mojibake?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9590913/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com