gpt4 book ai didi

函数不接受 c++ 参数

转载 作者:行者123 更新时间:2023-11-30 04:42:47 26 4
gpt4 key购买 nike

我的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);
^

有人知道我做错了什么以及解决方案吗?感谢您的帮助,我只是想编写一个简单的程序来测试一些概念,例如将指针传递给函数,但它似乎不起作用。

最佳答案

这段代码比 C++ 更像 C 风格。你正在做困难的容易出错的事情。尝试使用 vector 等

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);
}

关于函数不接受 c++ 参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58597808/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com