即使输入最少10个数字,我也没有错误,但我的代码在运行时立即崩溃.我也想知道,如果我有一个问题类似于我已经问过的另一个问题,我应该怎么做,但另一个新问题呢?
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max);
int main()
{
primer(5);
system("pause");
return 0;
}
int primer(int max){
vector<int> a;
a[1]=2;
for (int i=2;i<=max;i++){
bool prime=true;
for (int ii=0;ii<a.size();ii++) {
if (i/a[ii]==floor(i/a[ii])) {
prime=false;
}
}
if (prime==true) {
a.push_back(i);
}
}
for (int iii=0;iii<=a.size();iii++) {
cout << a[iii] << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我没有错误,但编译后的代码立即崩溃.我改成了
#include <iostream>
#include <cmath>
#include <fstream>
#include <cstdlib>
#include <vector>
using namespace std;
int primer(int max); …Run Code Online (Sandbox Code Playgroud) 将实现多个接口的对象传递给只需要其中一个接口的函数时,我遇到了构建错误.
我正在开发一个通讯包.该软件包有一个Receiver类和一个Sender类.Receiver类使用通知接口Receive_Notifier(函数对象)来处理通知.同样,Sender类使用通知接口Send_Notifier来处理通知.
interface Send_Notifier
{
void sending_text(string text);
}
interface Receive_Notifier
{
void raw_text_received(string raw_text);
}
class Sender
{
Send_Notifier m_notifier = null;
public Sender(ref Send_Notifier notifier)
{
m_notifier = notifier;
}
public void send(string text)
{
m_notifier.sending_text(text);
return;
}
}
class Receiver
{
Receive_Notifier m_notifier = null;
public Sender(ref Receive_Notifier notifier)
{
m_notifier = notifier;
}
public void receive(string text)
{
m_notifier.raw_text_received(text);
return;
}
}
Run Code Online (Sandbox Code Playgroud)
我已将接口组合到Communications_Notifier中:
interface Communications_Notifier
: Send_Notifier, Receive_Notifier
{
}
Run Code Online (Sandbox Code Playgroud)
我创建了一个Notifier实现Communications_Notifier接口的类:
class …Run Code Online (Sandbox Code Playgroud) 一直在浏览这段代码:
#include<cstdio>
#define TOTAL_ELEMENTS (sizeof(array) / sizeof(array[0]))
int array[] = {1,2,3,4,5,6,7};
int main()
{
signed int d;
printf("Total Elements in the array are => %d\n",TOTAL_ELEMENTS);
for(d=-1;d <= (TOTAL_ELEMENTS-2);d++)
printf("%d\n",array[d+1]);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
现在显然它没有进入for循环.什么原因?
我从静态分析工具中收到有关以下代码段的错误:
uint8_t value = 24U;
char buffer[512];
int chars_printed = snprintf(buffer, sizeof(buffer),
"The value in hex is 0x%02hhX\r\n",
value);
Run Code Online (Sandbox Code Playgroud)
错误是:
MISRA-2004规则10.1违规:隐式转换函数参数中的非常量表达式.将"value"与基础类型"unsigned char"(8位,无符号)转换为类型"int"(32位,有符号).
MISRA期望从"%X"说明符获得的签名和位宽是多少?
据说"%X" unsigned int来自cppreference页面.
IAR编译器的MISRA C 2004检查程序没有错误.
这个来自Coverity.
一个简单的问题:
当我将变量传递给函数时,程序是否会复制该函数中使用的变量?
如果它确实,并且我知道该函数只读取变量并且从不写入它,是否可以将变量传递给函数而不创建该变量的副本,或者我应该将其留给编译器优化来做那对我自动?
我修改了默认的Debug配置,以便输出目录类似$(SolutionDir)$(PlatformName)/$(ConfigurationName).
接下来,我创建了调试变体,DebugStatic并且DebugDll更明确地了解了正在创建的目标.我通过复制Debug配置创建了它们.
以类似的方式,我创建ReleaseDLL并ReleaseStatic从修改后的Release配置.
使用Configuration Manager,我删除了Debug和Release配置.
在Debug和Release配置仍然在显示批生成窗口以及在配置下拉框在属性页窗口(从右键点击项目名称显示出来,然后选择属性).
Debug和Release配置?(Debug的模糊性导致我解决了几周的问题,特别是在X64项目中意外组合Win32调试DLL时)
(我搜索了Web和StackOverflow,但没有发现任何关于完全删除这些配置的信息.)
c++ deployment configuration visual-studio-2008 visual-studio-debugging
我正在寻找编译时 C 字符串文字的长度。给出定义:
static const char * const header_left[] =
{
" | | Raw | Raw |",
" | | Start | End |",
"Interval#| Duration | Point | Point |",
"---------+----------+-------+-------+",
};
const unsigned int rows_in_header = sizeof(header_left) / sizeof(header_left[0]);
Run Code Online (Sandbox Code Playgroud)
如何header_left[2]在不使用 的情况下找到字符串文字的长度strlen?
在这个问题中,Determining the Length of a String Literal,有一条注释将数组声明为header_left[][4]。我不喜欢使用这种声明,因为字符串的数量有可能在不改变数量常量的情况下发生变化。我喜欢让编译器计算字符串的数量(参见定义rows_in_header)和每个字符串的长度。
这是针对嵌入式系统的,字符串被块写入串行端口。串口函数将指向数据的指针和数据的长度作为参数。串行端口代码针对块写入进行了优化。首选不使用,strlen因为这会浪费性能时间。
我在 ARM7TDMI 平台上使用 C99 和 IAR Embedded Workshop。
我包含了该c++标签,因为这也涉及 C++,我们将在首次产品发布后将代码迁移到 C++。
MISRA令我们的开发人员感到沮丧.
我们得到MISRA错误"不要将指针算法应用于指针"和"指针不指向数组".
我们使用以下语法:
uint8_t const * p_buffer
Run Code Online (Sandbox Code Playgroud)
将缓冲区传递给将缓冲区写入SPI总线的函数.
给出一个示例代码片段:
static void Write_Byte_To_SPI_Bus(uint8_t byte);
void Write_Buffer_To_SPI_Bus(uint8_t const * p_buffer,
unsigned int quantity)
{
for (unsigned int i = 0; i < quantity; ++i)
{
Write_Byte_To_SPI_Bus(*p_buffer++);
}
}
Run Code Online (Sandbox Code Playgroud)
有没有办法让一个指向数组中单元格的指针并增加它以满足MISRA?
我的解释是MISRA想要将索引递增到数组而不是指针:
void Write_Array_To_SPI_Bus(uint8_t const p_array[],
unsigned int quantity)
{
for (unsigned int i = 0; i < quantity; ++i)
{
Write_Byte_To_SPI_Bus(p_array[i]);
}
}
Run Code Online (Sandbox Code Playgroud)
许多开发人员都是老派,他们更喜欢使用指针uint8_t而不是传递数组.
我正在使用Orwell Dev C++ 5.8.3 IDE.它使用g ++ 4.8.1编译器.当我保存我的C++程序文件时,它会显示几个文件扩展名,用于保存我的C++程序文件.cpp, .cc, .cxx, .c++, .cp.
我选择哪个扩展名是否重要?
这些扩展是编译器/ IDE特定的还是特定于平台的?
我使用的是Windows 7操作系统.
这些扩展是否也可以在Unix或Linux等其他操作系统上使用和/或有效?
为什么提供这么多扩展?
是什么原因?
我搜索了这个问题,但我无法弄清楚如何解决这个问题:
class DtEffect;
template <typename VertexFormat>
class DtEffectRenderer : public DtFormatRenderer<VertexFormat>
{
public:
template <typename MemberType>
static DtEffect::VertexAttribPtrInfo VertexAttrib(const MemberType VertexFormat::* member)
{
return DtEffect::VertexAttribPtrInfo(
reinterpret_cast<const GLvoid*>(offsetof(VertexFormat, *member))
, DtAttributeType<MemberType>::value
, DtAttributeType<MemberType>::size);
}
protected:
DtEffect* myEffect;
};
Run Code Online (Sandbox Code Playgroud)
错误消息:
../../include/vrvGraphics/DtEffectRenderer.h: In static member function ‘static makVrv::DtEffect::VertexAttribPtrInfo makVrv::DtEffectRenderer<VertexFormat>::VertexAttrib(const MemberType VertexFormat::*)’:
../../include/vrvGraphics/DtEffectRenderer.h:115: error: expected primary-expression before ‘(’ token
../../include/vrvGraphics/DtEffectRenderer.h:116: error: expected unqualified-id before ‘*’ token
../../include/vrvGraphics/DtEffectRenderer.h:116: error: expected ‘)’ before ‘*’ token
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?