我从一个简单的cout没有输出,而printf将始终打印数字:
std::cout << variableuint8; // prints nothing
printf("%u", variableuint8); // prints the number
Run Code Online (Sandbox Code Playgroud)
我以前从未遇到过这种行为,虽然我有一个解决方法,但我想了解它.
耶,指针.所以它可能比所述的更多参与.这是上下文 - 我不认为它应该重要,当cout和printf获取信息时,它被解除引用到一个简单的unsigned char.这可能是解决问题所需的更多信息,但我想完全了解它是否相关.
typedef unsigned char UInt8;
typedef unsigned short UInt16;
typedef struct{
UInt8 len;
// some other definitions. sizeof(DeviceNotification) <= 8
}DeviceNotification;
UInt8 somerandomdata[8];
DeviceNotification * notification;
notification = (DeviceNotification *) somerandomdata;
std::cout << "Len: (" << notification->len << ")\n";
printf("Len: (%u)\n", notification->len);
Run Code Online (Sandbox Code Playgroud)
随机数据在别处初始化.对于此输出,数组中的第一个字节是0x08:
Len: ()
Len: (8)
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么这不起作用......
我在Linux工作
g ++什么都不做
gcc打印以下内容:
/tmp/ccyg7NDd.o: In function `main':
test.cc:(.text+0x14): undefined reference to `std::cout'
test.cc:(.text+0x19): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
test.cc:(.text+0x21): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
test.cc:(.text+0x29): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccyg7NDd.o: In function `__static_initialization_and_destruction_0(int, int)':
test.cc:(.text+0x51): undefined reference to `std::ios_base::Init::Init()'
test.cc:(.text+0x56): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccyg7NDd.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)
码:
#include<iostream> …
Run Code Online (Sandbox Code Playgroud) 无论出于何种原因,std :: cout都不会显示我的应用程序.我的开发环境如下所示.
我正在使用Qt Creator开发Qt应用程序.由于Qt Creator无法从我的工作站(XP64)启动,我目前正在使用Visual Studio 2008和Qt插件(通过导入.pro项目文件)进行开发.一切似乎都很好,应用程序工作.
在某些情况下(取决于命令行参数),我不想启动HIM,只是在CLI中显示几个句子(例如,命令行必需参数).
我没有收到任何错误,但没有显示任何内容.我相信运行的相应代码是(经典)以下:
std::cout << "is this going to be displayed ?" << std::endl;
Run Code Online (Sandbox Code Playgroud)
你知道为什么没有显示什么吗?
每次我'cout << endl'甚至'cout <<"\n"'然后在Windows下启动我的程序输出到文件("a.exe <test.in> result.out")我得到"\ r \n""result.out"中的行结尾.
在地球上是否有办法阻止它这样做,只在每个'cout <<"\n"'上输出"\n"?
提前致谢.
在搞乱代码时我遇到了这种相当模糊的行为,这是一个例子:
#include <iostream>
using namespace std;
int print(void);
int main(void)
{
cout << "The Lucky " << print() << endl; //This line
return 0;
}
int print(void)
{
cout << "No : ";
return 3;
}
Run Code Online (Sandbox Code Playgroud)
在我的代码中,带注释的语句//This line
应该打印出来The Lucky No : 3
,而是打印出来
No : The Lucky 3
.是什么导致这种行为?这是否与C++标准有关,或者它的行为因编译器而异?
我正在尝试编译下面的简单程序.但是,它没有编译并给出错误:
error C2065: 'cout' : undeclared identifier
Run Code Online (Sandbox Code Playgroud)
我想问你为什么这个程序不起作用虽然我已经包含iostream
头文件了?
#include <iostream>
void function(int) { cout << “function(int) called” << endl; }
void function(unsigned int) { cout << “function(unsigned int) called” << endl; }
int main()
{
function(-2);
function(4);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
提前致谢.
我经常cout
在我的代码中的许多不同位置用于调试目的,然后我感到沮丧并手动评论所有这些.
有没有办法在运行时抑制cout输出?
更重要的是,假设我想要抑制所有cout
输出,但我仍然希望在终端中看到1个特定输出(假设程序的最终输出).
是否可以使用""其他方式"打印到终端以显示程序输出,然后在抑制cout仍然看到使用这种"其他方式"打印的东西?
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <ctime>
int main(int argc, char* argv[])
{
std::clock_t start;
double duration;
std::cout << "Starting std::cout test." << std::endl;
start = std::clock();
for (int i = 0; i < 1000; i++)
{
std::cout << "Hello, World! (" << i << ")" << std::endl;
}
duration = (std::clock() - start) / (double) CLOCKS_PER_SEC;
std::cout << "Ending std::cout test." << std::endl;
std::cout << "Time taken: " << duration << std::endl;
std::system("pause");
std::cout << "Starting std::printf test." …
Run Code Online (Sandbox Code Playgroud) 我试图在c ++控制台应用程序中打印一个字符串到控制台.
void Divisibility::print(int number, bool divisible)
{
if(divisible == true)
{
cout << number << " is divisible by" << divisibleBy << endl;
}
else
{
cout << divisiblyBy << endl;
}
}
Run Code Online (Sandbox Code Playgroud)
我有正确的包含等,这个错误,我相信只是我只是不知道如何打印到c ++控制台,我想这不是这样做的方式
编辑:抱歉忘了提到divisiblyBy是字符串
源代码如下.
cout << '\\' << endl; //OK, output is \
cout << '\\\\' << endl; //OK, output is an integer 23644, but why?
Run Code Online (Sandbox Code Playgroud)
该语句cout << '\\\\' << endl;
调用类的以下函数ostream
.
_Myt& __CLR_OR_THIS_CALL operator<<(int _Val)
Run Code Online (Sandbox Code Playgroud)
我知道写表达式很奇怪'\\\\'
,但我不明白它为什么不会失败.如何解释结果?
c++ ×10
cout ×10
iostream ×2
printf ×2
char ×1
console ×1
function ×1
namespaces ×1
performance ×1
pointers ×1
qt ×1
qt-creator ×1
string ×1
visual-c++ ×1