C ++错误:[函数名]之前的预期初始值设定项

时间:2021-11-10 22:49:20

I am refreshing my self on C++ (have not did it since school) and I wrote a simple program just to mess around. My problem is when I compile the program it chokes stating "error: expected initializer before 'stringThing'" is there a reason why this is doing this? I know this may be a noob question so I checked * and could not find any relevant questions that gave me a answer.

我在C ++上刷新自己(从学校开始就没有这样做)我写了一个简单的程序只是乱七八糟。我的问题是,当我编译程序时,它会说“错误:在'stringThing''之前预期的初始化程序”是否有这样做的原因?我知道这可能是一个菜鸟问题所以我检查了*并找不到任何给我答案的相关问题。

*I am using GNU GCC compiler

*我正在使用GNU GCC编译器

Code:

#include <iostream>

using namespace std;

void string stringThing (string shiftdir, string &teststring)
    {
        if (shiftdir == "right")
        {
           teststring = teststring >> " " >> "Bit Shifted right";
        }
        else
        {
           teststring = teststring << " " << "Bit Shifted left";
        }
    }
int main()
{

    string test;

    cout << stringThing("right", "I have done a ") << endl;

    return 0;
}

2 个解决方案

#1


1  

The return type for stringThing must be either void or string, not both. You also must include <string>, if you want to use string.

stringThing的返回类型必须是void或string,而不是两者。如果要使用字符串,还必须包含

Since you want to output the return value of stringThing() in main, I guess it should be

既然你想在main中输出stringThing()的返回值,我想它应该是

std::string stringThing (std::string shiftdir, const std::string &teststring)

But then, you must also return a string from your function

但是,您还必须从函数中返回一个字符串

if (shiftdir == "right")
    return teststring + " " + "Bit Shifted right";
else
    return teststring + " " + "Bit Shifted left";

for example.

Your parameter std::string &teststring won't work with your const char* argument. So either declare it as a copy by value string only, or better const string&.

您的参数std :: string&teststring不能与您的const char *参数一起使用。因此,要么仅通过值字符串将其声明为副本,要么使用更好的const字符串&。

#2


3  

Return type is … funky

What is:

void string stringThing (string shiftdir, string &teststring)

?

Get rid of the first string. Your function returns nothing.

摆脱第一个字符串。你的函数什么都不返回

So, simply:

void stringThing(string shiftdir, string &teststring)

Inclusion missing

You will also need to #include <string> — in some scenarios you may get "lucky" and have it implicitly included by <iostream>, but don't rely on it.

您还需要#include - 在某些情况下,您可能会“幸运”并将其隐含地包含在 中,但不要依赖它。

#1


1  

The return type for stringThing must be either void or string, not both. You also must include <string>, if you want to use string.

stringThing的返回类型必须是void或string,而不是两者。如果要使用字符串,还必须包含

Since you want to output the return value of stringThing() in main, I guess it should be

既然你想在main中输出stringThing()的返回值,我想它应该是

std::string stringThing (std::string shiftdir, const std::string &teststring)

But then, you must also return a string from your function

但是,您还必须从函数中返回一个字符串

if (shiftdir == "right")
    return teststring + " " + "Bit Shifted right";
else
    return teststring + " " + "Bit Shifted left";

for example.

Your parameter std::string &teststring won't work with your const char* argument. So either declare it as a copy by value string only, or better const string&.

您的参数std :: string&teststring不能与您的const char *参数一起使用。因此,要么仅通过值字符串将其声明为副本,要么使用更好的const字符串&。

#2


3  

Return type is … funky

What is:

void string stringThing (string shiftdir, string &teststring)

?

Get rid of the first string. Your function returns nothing.

摆脱第一个字符串。你的函数什么都不返回

So, simply:

void stringThing(string shiftdir, string &teststring)

Inclusion missing

You will also need to #include <string> — in some scenarios you may get "lucky" and have it implicitly included by <iostream>, but don't rely on it.

您还需要#include - 在某些情况下,您可能会“幸运”并将其隐含地包含在 中,但不要依赖它。