简体   繁体   English

class header:错误:编译时“char”之前的预期不合格ID

[英]class header: error: expected unqualified-id before 'char' while compiling

Sorry for bad english.抱歉英语不好。 I am new on c++ and trying to understand to send references to class object.我是 c++ 的新手,并试图了解发送对 class object 的引用。 But I get above error.但我得到了上述错误。 If I use const char then error comes up for 'const' part.如果我使用 const char,则“const”部分会出现错误。 Here is my code:这是我的代码:

main:主要的:

    #include <iostream>
    #include "DelMe-ClassHeader.h"

    using namespace std;

    int main() {
        char var1 [2];
        int var2;

        for (int i = 0; i < 2; i++) {
            var1[i] = 2;
        }
        int var2 = 0;

        tc = TestClass(var1, var2);

        cout << "before tc.changeValue" << endl;
        cout << "var1 is " << var1 << endl;
        cout << "var2 is " << var2 << endl;

        tc.changeValue()

        cout << "before tc.changeValue" << endl;
        cout << "var1 is " << var1 << endl;
        cout << "var2 is " << var2 << endl;

    }

header: header:

    #ifndef TestClass
    #define TestClass

    #include <iostream>
    using namespace std;

    class TestClass {

        public:
            TestClass(char (& first)[2]}, int& second);
            void changeValue ();

        private:
            char (& privArray)[2];
            int& privInt;

    };

    #endif

cpp: cp:

    #include "DelMe-ClassHeader.h"

    #include <iostream>
    using namespace std;

    TestClass::TestClass(char (& first)[15], int& second) {
        this->priveArray = first;
        this->privInt = second;
    }

    void TestClass::changeValue () {
        privInt = atoi(privArray);
    }

and the error is:错误是:

    E:\Programing\CodeBlocks\Cpp\DelMe\DelMe\DelMe-ClassHeader.h|10|error: expected unqualified-id before 'char'

I gratefull for any help我很感激任何帮助

The header contains two errors: header 包含两个错误:

  1. #define TestClass defines TestClass as an empty string. #define TestClassTestClass定义为空字符串。 Therefore all occurences of TestClass will be replaced with an empty string, hence the errors.因此,所有出现的TestClass都将替换为空字符串,因此会出现错误。 For more information read about the C++ preprocessor.有关 C++ 预处理器的更多信息,请阅读。
  2. There is an extra } in the parameter list of TestClass . TestClass的参数列表中多了一个}

Replace with this:替换为:

#ifndef TestClass_h_inc_
#define TestClass_h_inc_

#include <iostream>
using namespace std;

class TestClass {

public:
  TestClass(char(&first)[2], int& second);

  void changeValue();

        private:
          char(&privArray)[2];
          int& privInt;

    };

#endif

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM