我的c++程序是这样的:
#include <iostream>
#include <cstdlib>
#include <dos.h>
using namespace std;
void arr(int pts,string *splt_msg){
for(int x=-1;x<pts-1;x++){
string input;
cout<<"Enter part "<<x<<endl;
cin>>input;
(*splt_msg)[x]=input;
}
}
void print_arr(string *splt_msg,int del,int parts){
for(int x=-1;x<parts-1;x++){
cout<<(*splt_msg)[x];
delay(del);
}
}
int main(){
cout<<"Parts in message: ";
int parts;
cin>>parts;
cout<<"Delay between parts(ms): ";
int del;
cin>>del;
parts--;
string splt_msg[parts];
string (*Parr)[parts]=&splt_msg;
arr(parts,Parr);
print_arr(Parr,del,parts);
return 0;
}以下是错误代码(在windows和mingw上):
c:\Users\User\Desktop\c++>g++ -o program test1.cpp
In file included from test1.cpp:3:0:
c:\mingw\include\dos.h:54:2: warning: #warning "<dos.h> is obsolete; consider using <direct.h> instead." [-Wcpp]
^~~~~~~
test1.cpp: In function 'void arr(int, std::__cxx11::string*)':
test1.cpp:18:14: error: 'delay' was not declared in this scope
delay(del);
^
test1.cpp: In function 'int main()':
test1.cpp:32:17: error: cannot convert 'std::__cxx11::string (*)[parts] {aka std::__cxx11::basic_string<char> (*)[parts]}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' for argument '2' to 'void arr(int, std::__cxx11::string*)'
arr(parts,Parr);
^
test1.cpp:33:27: error: cannot convert 'std::__cxx11::string (*)[parts] {aka std::__cxx11::basic_string<char> (*)[parts]}' to 'std::__cxx11::string* {aka std::__cxx11::basic_string<char>*}' for argument '1' to 'void print_arr(std::__cxx11::string*, int, int)'
print_arr(Parr,del,parts);
^有没有人知道我做错了什么和解决办法?谢谢你的帮助,我只是想写一个简单的程序来测试一些概念,比如将指针传递给函数,但它似乎不起作用。
发布于 2019-10-29 04:49:24
这段代码更像是C语言风格,而不是C++。你正在做一些容易出错的困难的事情。试着使用向量之类的
string splt_msg[parts];甚至不应该求值,因为parts不是常量表达式。
尝试使用std::vector之类的东西。并通过引用传递。例如:
#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>
template<typename T>
T Read() noexcept {
T output{};
std::cin >> output;
return output;
}
void arr(std::vector<std::string>& splt_msg) {
for (auto& msg : splt_msg) {
std::cout << "Enter part \n";
std::cin >> msg;
}
}
void print_arr(std::vector<std::string> const& splt_msg, int delay) {
for (auto& msg : splt_msg) {
std::cout << msg;
Sleep(delay);
}
}
int main() {
std::cout << "Parts in message: ";
auto parts = Read<int>();
std::cout << "Delay between parts(ms): ";
auto delay = Read<int>();
std::vector<std::string> splt_msg(parts);
arr(splt_msg);
print_arr(splt_msg, delay);
}编辑:或者更好:使用对象
#include <iostream>
#include <string>
#include <vector>
#include <Windows.h>
template<typename T>
T Read() noexcept {
T output{};
std::cin >> output;
return output;
}
class SplitMessage {
private:
std::vector<std::string> splitMessage;
public:
SplitMessage(size_t length) noexcept : splitMessage(length) {}
void GetData() noexcept;
void PrintData(int delay) const noexcept;
};
void SplitMessage::GetData() noexcept {
for (auto& msg : splitMessage) {
std::cout << "Enter part \n";
std::cin >> msg;
}
}
void SplitMessage::PrintData(int delay) const noexcept {
for (auto& msg : splitMessage) {
std::cout << msg;
Sleep(delay);
}
}
int main() {
std::cout << "Parts in message: ";
auto parts = Read<int>();
std::cout << "Delay between parts(ms): ";
auto delay = Read<int>();
SplitMessage splt_msg(parts);
splt_msg.GetData();
splt_msg.PrintData(delay);
}发布于 2019-10-29 04:43:30
这两个函数都需要一个std::string* (指向字符串的指针)作为第一个参数,但两次都需要传递std::string*[] (指向字符串的指针数组)作为第一个参数。解决方案取决于您想要实现什么,但我猜,您希望使用索引器为数组的一个元素调用函数:print_arr(Parr[0],del,parts);
其他一些注意事项:
for(int x=-1;x<parts-1;x++){cout<<(*splt_msg)[x];...}将访问字符串的第-1个字符,即UB,但可能会崩溃。for(int x=0;x<parts;x++)string splt_msg[parts];不是有效的C++,因为parts在编译时不是常量。https://stackoverflow.com/questions/58597808
复制相似问题