c ++课不说话

时间:2013-09-28 23:09:31

标签: c++ class

我正在使用gvim为一个类做c ++(因为我们被告知我们必须在linux中完成它,并且该类是用c ++教授的)。我之前有过java课程,但这位老师并没有真正告诉我们如何在c ++或linux中做事,因为他说它是另一个只使用c ++的类。

我的功课问题是我们必须创建一些类并让它们互相获取信息,但每当我尝试编译时我都会遇到错误。 (我似乎无法弄清楚如何让他们彼此交谈并使用彼此的功能/变量。)

例如:

class a {

string user;
public: string user2;
public: vector<string> friendList;

public: void userName()
{
    cout output
    cin >> user;
}
public: void addFriend()
{
    cout output
    cin >> user2;
    friendList.push_back(user2);
}

public: string getName()
{
    return user;
}
};

(已尝试过这第二课2种方式而且都不起作用) WAY1 ---&GT;

class b {
string message, username;
a User;
public: void postMessage()
    {
        cout ____
        getline(cin, message);

        username = User.getName();
    }
};

或这样----&gt;

class b: public a {
string message, username;
a User;
public: void postMessage()
    {
        cout ____
        getline(cin, message);

        username = User.getName();
    }
};

(或者有这样的功能:)

    public: void postMessage()
    {
        cout ____
        getline(cin, message);

        username = user2;
    }
};
这些课程似乎没有相互交谈,我不知道如何让他们去,因为这些方法不起作用,这就是书中的内容/我在互联网上找到的东西所以远。

所以我想我的问题是如何才能与b交谈,所以b可以使用函数或变量?需要知道所以类可以互相交谈,所以我也可以让main函数也调用每个类(每个类都在一个单独的.cpp文件中)。

编辑:

(这些类在不同的文件中)

我为终端制作了一个错误的脚本:

Script started on Sun 29 Sep 2013 02:27:42 PM CDT
]0;darksithis002@darkmist002-VirtualBox: ~/lab1darksithis002@darkmist002-VirtualBox:~/lab1$ g++ -c  homepg.cpp
homepg.cpp:14:26: error: expected class-name before ‘,’ token
homepg.cpp:15:1: error: expected class-name before ‘{’ token
homepg.cpp:18:24: error: ‘user’ was not declared in this scope
homepg.cpp:18:24: error: ISO C++ forbids initialization of member ‘userName1’ [-fpermissive]
homepg.cpp:18:24: error: making ‘userName1’ static [-fpermissive]
homepg.cpp:18:24: error: invalid in-class initialization of static data member of non-integral type ‘std::string {aka std::basic_string<char>}’
homepg.cpp:19:19: error: ISO C++ forbids initialization of member ‘counter’ [-fpermissive]
homepg.cpp:19:19: error: making ‘counter’ static [-fpermissive]
homepg.cpp:19:19: error: ISO C++ forbids in-class initialization of non-const static member ‘counter’
homepg.cpp:20:30: error: ‘friendList’ was not declared in this scope
homepg.cpp:20:30: error: ISO C++ forbids initialization of member ‘friends’ [-fpermissive]
homepg.cpp:20:30: error: making ‘friends’ static [-fpermissive]
homepg.cpp:20:30: error: invalid in-class initialization of static data member of non-integral type ‘std::vector<std::basic_string<char> >’
homepg.cpp:22:5: error: ‘cout’ does not name a type
homepg.cpp:23:5: error: ‘cout’ does not name a type
homepg.cpp:24:5: error: ‘cout’ does not name a type
homepg.cpp:29:26: error: ISO C++ forbids declaration of ‘displayHome’ with no type [-fpermissive]
homepg.cpp: In member function ‘int homepg::displayHome()’:
homepg.cpp:31:12: error: ‘messageBuff’ was not declared in this scope
homepg.cpp:45:6: error: ‘nextbrac’ was not declared in this scope
homepg.cpp:53:18: error: ‘userName’ was not declared in this scope
homepg.cpp:64:28: error: ‘friends’ was not declared in this scope
homepg.cpp:85:6: error: ‘count’ was not declared in this scope
]0;darksithis002@darkmist002-VirtualBox: ~/lab1darksithis002@darkmist002-VirtualBox:~/lab1$ g++ -c    messageBuffer.cpp
 messageBuffer.cpp: In member function ‘void messageBuffer::postMessage()’:
 messageBuffer.cpp:26:13: error: ‘user’ was not declared in this scope
 messageBuffer.cpp: In member function ‘void messageBuffer::tweetMessage()’:
 messageBuffer.cpp:45:17: error: ‘user’ was not declared in this scope
]0;darksithis002@darkmist002-VirtualBox: ~/lab1darksithis002@darkmist002-VirtualBox:~/lab1$ exit
 exit

 Script done on Sun 29 Sep 2013 02:29:16 PM CDT

作为一个测试,我把我的两个类放在一起,一个在它自己编译好,一个需要一个函数/变量从另一个类,并尝试编译,他们编译得很好

class a {

*functions and variables for a* }

 class b {
 a A;
 *functions for b* }

这个例子中的b类只是在我尝试调用a的同一目录中的单独文件中不起作用,它给了我在我制作的脚本中得到的错误。

EDIT2:如果我输入一个main函数并让它调用a和b类的函数,我也会在测试文件中出错。 (我知道这个错误来自于没有声明函数,但是如果我尝试在他们的类中声明它们,它们会给我另一个错误:type :: functionname不能重载,但如果我取出声明a和b编译好了,b可以使用函数,那么为什么没有函数声明它们可以正常但主函数不能?如果我不能在类中使用它们,我在哪里可以放置函数的声明,因为它说它不能超载它们吗?)

2 个答案:

答案 0 :(得分:2)

首先 - 你只需要写一次“public:”一次,之后一切都是公开的(直到你写“private:”或类似的)。

第二 - 你的第一个“b级”似乎是正确的。

第三 - 这两个类都写在同一个文件中吗?或者在不同的文件中?如果在不同的文件中 - 您是否在a的文件中包含了b的文件?它们是.h个文件还是.cc个文件?

编辑:这是应该如何,暂时说一个文件:

一个文件,main.cc:

#include <string>
#include <vector>
#include <iostream>
using namespace std;

class a {
  string user;
public:
  string user2;
  vector<string> friendList;

  void userName()
  {
      cout <<"blah blah"<<endl;
      cin >> user;
  }
  void addFriend()
  {
      cout <<"blah blah"<<endl;
      cin >> user2;
      friendList.push_back(user2);
  }

  string getName()
  {
      return user;
  }
};

class b {
  string message, username;
  a User;
public: 
  void postMessage()
  {
      cout <<"blah blah"<<endl;
      getline(cin, message);

      username = User.getName();
  }
};

int main(){
  b test;
  test.postMessage();
}

你的课程有很多问题 - 但至少应该编译。

答案 1 :(得分:1)

以下是一些让你感动的提示!

为每个文件定义一个class,并在 包含警卫 中包装头文件(*.h)。

档案a.h

#ifndef A_H
#define A_H

#include <string>
#include <vector>
#include <iostream>

class a // This class needs a better name!
{
private: // Class members are private by default but it's best to be explicit
    std::string user; // When using classes from the C++ Standard Library in a 
                      // header file, specify the `std` namespace explicitly

public: // Everything below here is public
    std::vector<std::string> friendList;

    void userName()
    {
        std::cout << "User name: "; // Note the use of `std::` here otherwise you'll see
                                    // ‘cout’ does not name a type
        std::cin >> user;
    }

    void addFriend()
    {
        std::cout << "Add friend: ";
        std::string user2;
        std::cin >> user2;
        friendList.push_back(user2);
    }

    std::string getName()
    {
        return user;
    }
};

#endif

档案b.h

#ifndef B_H
#define B_H

#include <string>
#include <iostream>
#include "a.h" // Tell the pre-processor to include the entire contents of `a.h`,
               // so that the code below knows what `class a` is.

class b 
{
private:
    std::string message, username;
    a User;

public: 
    void postMessage()
    {
        std::cout << "Post Message: ";
        std::getline(std::cin, message);

        username = User.getName();
    }
};

#endif

档案main.cpp

这是您需要编译的唯一文件,因为它包含b.h(以及a.h)。

#include "b.h"

int main()
{
    b my_b;
    my_b.postMessage();
}

在上面的代码中,与“方式1”类似,class b 包含class a 的实例。

如果您希望class b 扩展class a ,例如“Way 2”,您可以使用以下内容:

class b: public a
{

// [...]

public: 
    void postMessage()
    {
        std::cout << "Post Message: ";
        std::getline(std::cin, message);

        username = getName();
    }
};

编译

使用终端:

g++ -I/usr/local/include -Wall -Wextra -pedantic main.cpp -o main

或使用像SingleCompile这样的Vim脚本。