如何在C++中创建一个类,在初始化时,在调用其名称时返回一个布尔值,但没有显式函数调用make,如ifstream.我希望能够这样做:
objdef anobj();
if(anobj){
//initialize check is true
}else{
//cannot use object right now
}
Run Code Online (Sandbox Code Playgroud)
不只是初始化,而是检查其使用能力.
不要问我怎么样 - 我没有丝毫.
以下代码崩溃了我的终端和我使用的任何运行时分析工具,同时没有引发任何静态检查工具警告.Valgrind,cppcheck和gdb对我没那么好.g ++ -Wall不会给我任何有用的东西.
代码的目标是通过USB串行连接将字符写入audrino.audrino地址作为第一个参数传递.传递unsigned int并将其强制转换为unsigned char.
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <termios.h>
#include <iostream>
using namespace std;
int main(int argc,char** argv){
struct termios stdio;
int tty_fd;
unsigned char ctrlChar;
unsigned int ctrlInt;
tty_fd=open(argv[1], O_RDWR | O_NONBLOCK);
tcgetattr(tty_fd, &stdio);
cfmakeraw(&stdio);
stdio.c_cc[VMIN] = 1;
stdio.c_cc[VTIME] = 0;
tcsetattr(tty_fd,TCSANOW, &stdio);
tcsetattr(tty_fd,TCSAFLUSH, &stdio);
fcntl(tty_fd, F_SETFL, fcntl(STDIN_FILENO, F_GETFL, 0) | O_NONBLOCK);
cfsetospeed(&stdio,B9600); // 9600 baud
cfsetispeed(&stdio,B9600); // 9600 baud
cout << "ready on " << argv[1] …
Run Code Online (Sandbox Code Playgroud) 这是代码.我不知道它为什么不承认它需要复制内存,我不能强迫它.
string message="The quick brown fox jumped over the lazy dog.";
vector<char*> words;
while(message.length()>0){
char wtf[message.substr(0,message.find(" ")).c_str().length()]=message.substr(0,message.find(" ")).c_str();
words.push_back(wtf);
message=message.substr(message.find(" ")+1);
}
Run Code Online (Sandbox Code Playgroud)
我看到有类似的线程,但没有.而且,C++似乎无法轻易解决这个问题.