【发布时间】:2020-05-24 18:06:31
【问题描述】:
创建了默认的 qt 小部件项目:
#include "mainwindow.h"
#include <string>
#include <cstring>
#include <QApplication>
#include <iostream>
#include <QDir>
int main(int argc, char *argv[])
{
std::string s = QDir::currentPath().toLocal8Bit().constData();
std::string s2 = QDir::currentPath().toLocal8Bit().constData();
std::string s3 = "dsdasda";
int t = s.size();
char str[1024]= { 0 };
char str2[1024]= { 0 };
std::memcpy(str,s.c_str(),s.size ());
std::memcpy(str2,&s,s.size ());
int i = 0;
while(str[i]) std::cout << str[i++] ;
char * pch;
pch = strtok (str,"/");
while (pch != NULL)
{
printf ("%s\n",pch);
pch = strtok (NULL, "/");
}
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}
还有我的 .pro 文件:
QT += core gui network widgets
CONFIG += c++11
# The following define makes your compiler emit warnings if you use
# any Qt feature that has been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
# You can also make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000 # disables all the APIs deprecated before Qt 6.0.0
SOURCES += \
main.cpp \
mainwindow.cpp
HEADERS += \
mainwindow.h
FORMS += \
mainwindow.ui
# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target
粘贴 main.cpp 只是因为其他文件是默认文件,不要参考我的问题。
这是错误文本(或者可能是警告):
main.cpp:11:11: error: no type named 'string' in namespace 'std'
main.cpp:17:11: error: no member named 'memcpy' in namespace 'std'
main.cpp:3:10: error: 'cstring' file not found
和其他一样。
我不知道为什么会有这么多错误(警告?),为什么如果有任何错误它会编译和启动以及如何修复所有这些错误。
【问题讨论】:
-
char str[1024]= { 0 }; char str2[1024]= { 0 }; std::memcpy(str,s.c_str(),s.size ()); std::memcpy(str2,&s,s.size ());- 为什么?为什么你会想要那样做?你为什么要把那些漂亮的QStrings 转换成std::strings? -
您的构建是否可以优化?我不知道如何 QMake,但你的构建命令中应该有类似
-Og -ggdb的内容,绝对不是-O1、-O2等。