我尝试从传感器读取数据。传感器控制器使用 db9 标头 (com1),因为我将使用 com1,所以我使用 db9 到 USB 转换器并获取 com 11。
我有程序读取和写入串口,当我使用com1时它可以工作,但是当我更改为com 11时,程序无法打开com,因为它到达了ERROR_FILE_NOT_FOUND
这是我的串口编程程序:
Serial::Serial(char *portName)
{
this->connected = false;
wchar_t wcPort[64];
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wcPort, strlen(portName), portName, _TRUNCATE);
this->hSerial = CreateFile(wcPort,
GENERIC_READ | GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
0);
//Check if the connection was successfull
if(this->hSerial==INVALID_HANDLE_VALUE)
{
//If not success full display an Error
if(GetLastError()==ERROR_FILE_NOT_FOUND){
//Print Error if neccessary
printf("ERROR: Handle was not attached. Reason: %s not available.\n", portName);
}
else
{
printf("ERROR!!!");
}
}
else …Run Code Online (Sandbox Code Playgroud) 假设我创建std::vector<int*> myVec;并保留100个条目,并使用值填充向量,以便所有100个元素都具有有效的指针。然后,我缓存一个指向其中一个元素的指针,
int * x = myVec[60];
Run Code Online (Sandbox Code Playgroud)
然后,如果我追加另一个int *由于堆碎片而触发大小调整以及移动的操作,则指向该指针的先前指针会失效还是指向内存中的新位置?
如果我的内存服务器正确,则示例在std::vector<int> myVecTwo与上述相同的条件下运行,并且我存储了ptr
int * x = &myVecTwo[60];
Run Code Online (Sandbox Code Playgroud)
并继续追加和调整大小,该指针将无效。
因此,我的问题如下。指向指针的指针会失效吗?由于is_trivially_copyable的新C ++ std功能以及std :: vector是否利用此类功能或POD检查,我不再确定。
指针在C ++ 11中会无效吗?
我正在尝试构建一个值备忘录,使用 unordered_map 将一对整数链接到一个整数。
int memo(pair<int, int> &p, unordered_map<pair<int, int>, int> &memo) {
if (memo[p] != -1) {
}
}
Run Code Online (Sandbox Code Playgroud)
我以为我会用pair来访问unordered_map中的值,但我收到了一个错误:
与“operator[]”不匹配(操作数类型为“std::unordered_map<std::pair<int, int>, int>”和“std::pair<int, int>”)
当尝试验证固定长度字符串数组在编译时是否已排序时,使用 时会出现奇怪的行为strncmp。
如果验证函数引用全局数组,则 N 的所有值似乎都有效。
#include <cstring>
#define N 8 // vary values of N
const char STRINGS[][N] = {"a", "b", "c"};
constexpr bool is_sorted_global() {
for (int i = 0; i < sizeof(STRINGS) / N - 1; i++) {
if (strncmp(STRINGS[i], STRINGS[i + 1], N) > 0) {
return false;
}
}
return true;
}
int main()
{
// always works for any N
static_assert(is_sorted_global(), "list is not sorted");
}
Run Code Online (Sandbox Code Playgroud)
但是,如果使用函数模板,则只有 N 小于或等于 8 的值才有效。
template<const char …Run Code Online (Sandbox Code Playgroud) 在 C 中组合类型说明符时是否有预期的顺序?例如 short,,,,如果顺序有意义,那么改变long类型修饰符的顺序会产生什么效果?signedunsigned
例如,有时我看到人们使用long unsigned int,其他时候unsigned long int。这些意思是一样的吗?
我有以下代码:
A = rand(N1,N2);
b = rand(1,N1);
B = zeros(N1,N2);
for i=1:N1
for j=1:N2
B(i,j) = A(i,j)*b(i);
end
end
Run Code Online (Sandbox Code Playgroud)
问题是如何用矢量运算形式编写它?有点像B(:,:) = A(:,:).*b(:).
我正在通过引用练习类声明/实现/传递对象.这是我的主类,头文件和其他类的代码:
//Header file (Circle2.h)
#ifndef CIRCLE_H
#define CIRCLE_H
class Circle{
private:
double radius;
public:
Circle();
Circle(double newRadius);
double getArea() const;
double getRadius() const;
void setRadius(double newRadius);
};
#endif
//Main class (PassObjectByReference.cpp)
#include <iostream>
#include "Circle2.h"
using namespace std;
void printCircle(const Circle &c){
cout << "The area of the circle of " << c.getRadius() << " is " << c.getArea() << endl;
}
int main(){
Circle myCircle(5.0);
printCircle(&myCircle);
return 0;
}
//Other class (Circle2.cpp)
#include "Circle2.h"
Circle::Circle(){
radius = 1;
} …Run Code Online (Sandbox Code Playgroud) 我的代码中有以下行:
char y[] = "a";
scanf("Letter: %s", y);
printf("%s\n", y);
Run Code Online (Sandbox Code Playgroud)
第二行根本不影响第三行的输出.我已经包括在内<stdio.h>,我想不出有什么问题......
我一直在努力编译这个程序,但是我一直想出错误:"期待'''在'''token'之前,在函数1被声明的底部附近..我在那里的代码对我来说很好看所以我我真的不太确定发生了什么..请对此有所了解,我几乎是C的菜鸟所以请你好...
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#define maxrow 20 //defines maxrow as a constant of 20
#define maxcol 30 //defines maxcol as a constant of 30
void function1(char array[][maxcol]);
void function4(int, int); //function to display the pairs count
int main( void )
{
int x = 0;
int y = 0;
int row = 0;
int col = 0;
int countX = 0;
int countY = 0;
srandom( (unsigned) time(NULL) );
char array[maxrow][maxcol];
function1(array);
/*
for (x=0;x<maxrow;x++)
{
for (y=0;y<maxcol;y++)
{
array[x][y] …Run Code Online (Sandbox Code Playgroud) 我想实现一个成员函数,如下所示:
void X() {}
class Foo
{
static void(*Bar)() = X;
};
Run Code Online (Sandbox Code Playgroud)
这不会编译:
错误:非整数类型的静态数据成员“void (* Foo::Bar)()”的类内初始化需要“constexpr”
我知道这是不合法的。我必须在类范围之外初始化 Bar 或将其设置为“内联静态”。问题是后者是 C++17 功能,而我必须使用 C++11(BCC32X 限制)。所以我的问题是:有没有办法在同一行上做到这一点?也许使它成为常量?我知道我们可以做到这一点(来源)...
class Foo
{
static int const i = 42;
}
Run Code Online (Sandbox Code Playgroud)
但我们能以某种方式将它应用到函数中吗?
PD:我知道我的问题有无限的解决方案,但到目前为止,我所看到的所有解决方案最终都依赖于我无法使用的后来的 C++ 功能。