我有编译GNUARM编译器的代码,但Visual Studio 2010发出错误.该问题涉及在C语言文件中的第一个语句之后声明变量:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 6;
i = i + 1;
printf("Value of i is: %d\n", i);
int j = i * 10; // <-- This is what Visual Studio 2010 complains about.
printf("Value of j is: %d\n", j);
return EXIT_SUCCESS;
}
Run Code Online (Sandbox Code Playgroud)
以下代码编译时没有错误:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
int i = 6;
int j; // <-- Declaration is now here, valid according to K&R rules.
i = i + …Run Code Online (Sandbox Code Playgroud) 在C++和Java,数据结构可以具有private,public和protected区域.我想将这个概念移植到我正在编写的C语言程序中.
是否有任何习惯用于在C中实现私有或受保护的函数指针和数据字段struct?
我知道C struct是公开的,我正在寻找一个成语来帮助隐藏一些实现细节并迫使用户使用公共接口.
注意:该商店已选择该语言,因此我无法将面向对象的概念应用到C.
谢谢.
我有一个表使用3个外键到其他表.当我执行左连接时,我得到重复的列.MySQL表示USING语法将减少重复列,但没有多个键的示例.
鉴于:
mysql> describe recipes;
+------------------+------------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------------+------------------+------+-----+---------+-------+
| ID_Recipe | int(11) | NO | PRI | NULL | |
| Recipe_Title | char(64) | NO | | NULL | |
| Difficulty | int(10) unsigned | NO | | NULL | |
| Elegance | int(10) unsigned | NO | | NULL | |
| Quality | int(10) unsigned | NO | | NULL | …Run Code Online (Sandbox Code Playgroud) 我看到有可能这样做,但我不明白这个兴趣.
在访问者模式的典型实现中,类必须考虑基类的所有变体(后代).在许多情况下,访问者中的相同方法内容应用于不同的方法.在这种情况下,模板化的虚拟方法将是理想的,但目前,这是不允许的.
鉴于(基础):
struct Visitor_Base; // Forward declaration.
struct Base
{
virtual accept_visitor(Visitor_Base& visitor) = 0;
};
// More forward declarations
struct Base_Int;
struct Base_Long;
struct Base_Short;
struct Base_UInt;
struct Base_ULong;
struct Base_UShort;
struct Visitor_Base
{
virtual void operator()(Base_Int& b) = 0;
virtual void operator()(Base_Long& b) = 0;
virtual void operator()(Base_Short& b) = 0;
virtual void operator()(Base_UInt& b) = 0;
virtual void operator()(Base_ULong& b) = 0;
virtual void operator()(Base_UShort& b) = 0;
};
struct Base_Int : public Base …Run Code Online (Sandbox Code Playgroud) c++ templates virtual-functions operator-overloading visitor
打印日志时,我希望每条消息都有一个时间戳,测量自程序启动以来的时间。最好以纳秒为单位,尽管毫秒也可以:
( 110 ns) Some log line
( 1220 ns) Another log line
( 2431 ns) Now for some computation...
(10357 ns) Error!
Run Code Online (Sandbox Code Playgroud)
据我了解,C++ chrono 库中有三种不同的时钟和另外两种 C 风格的时钟:
对于上述任务,每种方法的优点和缺点是什么?
我是c ++的初学者,并认为我要学习的唯一方法就是弄脏一些代码.我正在尝试构建一个连接到mysql数据库的程序.我在linux上使用g ++.没有想法.
我运行"make",这是我的错误:
hello.cpp:38: error: ‘get_driver_instance’ is not a member of ‘sql::mysql’
make: *** [hello.o] Error 1
Run Code Online (Sandbox Code Playgroud)
这是我的代码,包括makefile.任何帮助都会很棒!提前致谢
###BEGIN hello.cpp###
#include <stdlib.h>
#include <iostream>
#include <sstream>
#include <stdexcept>
#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>
#define EXAMPLE_HOST "localhost"
#define EXAMPLE_USER "root"
#define EXAMPLE_PASS ""
#define EXAMPLE_DB "world"
using namespace std;
using namespace sql::mysql;
int main(int argc, const char **argv)
{
string url(argc >= 2 ? argv[1] : EXAMPLE_HOST);
const string user(argc >= 3 ? …Run Code Online (Sandbox Code Playgroud) 我在网上搜索并阅读了Boost文档shared_ptr.在SO上有一个响应,表示shared_ptr对于写入时复制(COW)糟透了,TR!并且已将其从字符串库中删除.关于SO的大多数建议都说使用shared_ptr而不是常规指针.
该文档还讨论了使用std::unique()COW指针,但我没有找到任何示例.
是否有关于为您执行COW的智能指针或让您的对象使用新shared_ptr的克隆对象然后修改克隆对象的讨论?
struct Nutrients;
struct Ingredient
{
Ingredient(const std::string& new_title = std::string(""))
: m_title(new_title)
{ ; }
std::string m_title;
Nutrients ing_nutrients;
};
struct Milk : public Ingredient
: Ingredient("milk")
{ ; }
struct Cream : public Ingredient
: Ingredient("cream")
{ ; }
struct Recipe
{
std::vector< boost::shared_ptr<Ingredient> > m_ingredients;
void append_ingredient(boost::shared_ptr<Ingredient> new_ingredient)
{
m_ingredients.push_back(new_ingredient);
return;
}
void replace_ingredient(const std::string& original_ingredient_title,
boost::shared_ptr<Ingredient> new_ingredient)
{
// Confusion here …Run Code Online (Sandbox Code Playgroud) 我知道这个算法是如何工作的,但是不能决定何时使用哪种算法?
是否有一些指导方针,哪一个比其他或任何考虑更好?
非常感谢.
我正在寻找一种简单的方法来使用流从文本文件中读取十六进制值.我使用"C++十六进制读取流0x"搜索了Stack Overflow,大多数响应是关于将十六进制写为文本或以十六进制值读取而没有"0x"前缀.这个问题是关于读取十六进制数,在一次操作中使用"0x"前缀作为数字.
我的方法:
unsigned char byte;
std::istringstream sample("0xce");
sample >> std::hex >> byte;
Run Code Online (Sandbox Code Playgroud)
结束时byte从第一个字符开始包含'0'(0x30).
'strtol`函数处理转换,但需要读取数据,转换为C风格的字符串.
我operator>>在类中重载以读取逗号分隔值(CSV)文本文件.以下是数据文件的示例:
1,-4.93994892,0xa5,8,115.313e+3,
2,-4.93986238,0xc0,8,114.711e+3,
3,-4.93977554,0xc2,8,114.677e+3,
Run Code Online (Sandbox Code Playgroud)
我的提取方法:
class Csv_Entry
{
public:
friend std::istream& operator >>(std::istream& inp, Csv_Entry& ce);
unsigned int m_index;
double m_time;
unsigned char m_byte;
unsigned int m_data_length;
double m_bit_rate;
};
std::istream&
operator >> (std::istream& inp, Csv_Entry& ce)
{
char separator;
inp >> ce.m_index;
inp >> separator;
inp >> ce.m_time;
inp >> separator;
inp >> std::hex >> ce.m_byte;
inp >> separator;
inp …Run Code Online (Sandbox Code Playgroud)