【问题标题】:Ttrying to understand Classes and headers试图理解类和标题
【发布时间】:2016-07-18 17:14:30
【问题描述】:

**问题已解决。看来我错误地创建了一个额外的标题,并且由于我删除了他,所以它起作用了。 **

所以我试图了解类和标题以及如何使它们一起工作。 我正在关注在线教程,但我的代码似乎出了点问题。 问题是当我尝试运行main 时,它给了我这个错误: Cat::speak() 和所有其他函数的多重定义。

main.cpp

#include <iostream>
#include "class.h"
using namespace std;

int main()
{
    Cat jim;
    jim.makehappy();
    jim.speak();

    Cat george;
    george.makesad();
    george.speak();

    return 0;
}

class.cpp

#include <iostream>
#include "class.h"
using namespace std;

void Cat::speak()
{
    if (happy)
    {
        cout << "meoww" << endl;
    }
    else
    {
        cout << "sssss" << endl;
    }
}

void Cat::makehappy()
{
    happy = true;
}

void Cat::makesad()
{
    happy = false;
}

class.h

#ifndef CLASS_H_INCLUDED
#define CLASS_H_INCLUDED

class Cat
{
private:
    bool happy;
public:
    void makehappy();
    void makesad();
    void speak();
};

#endif // CLASS_H_INCLUDED

【问题讨论】:

  • 你是如何编译你的代码的?或者更确切地说是链接,看起来您在链接期间不止一次使用 class.cpp。
  • 但是`错误:多重定义`并不表示链接器问题..
  • 我试过你的代码,编译或链接没有错误。你建设得怎么样了?
  • im 使用 gnu 编译器和代码块。
  • 你能得到一个构建控制台并发布一个完整构建的内容吗?

标签: c++ header


【解决方案1】:

你是如何编译代码的?您需要确保在将它们链接在一起之前分别构建特定的“class.o”和“main.o”文件。这是一个示例 Makefile。

all: main

main: main.o class.o
    g++ main.o class.o -o main

main.o: main.cpp class.h
    g++ -c main.cpp

class.o: class.cpp class.h
    g++ -c class.cpp

看起来您正在使用双重包含防护,所以我认为这不是问题。查看此答案以更深入地解释正在发生的事情:Error with multiple definitions of function

【讨论】:

    【解决方案2】:

    从您在此处显示的内容来看,应该没有问题。你可以做些什么来临时解决这个问题,看看你是否真的在几个地方定义了这个函数,就是将你的类包装在一个命名空间中。

    class.h

    #ifndef CLASS_H_INCLUDED
    #define CLASS_H_INCLUDED
    
    namespace myNamespace {
    
    class Cat {
    private:
        bool happy;
    public:
        void makehappy();
        void makesad();
        void speak();
    };
    
    } // namespace myNamespace
    
    #endif // CLASS_H_INCLUDED
    

    class.pp

    #include <iostream>
    #include "class.h"
    
    // using namespace std; // Don't Use - This is Bad Practice
                            // Can cause name clashing when trying to resolve name lookup
    
    
    namespace myNamespace {
    
    void Cat::speak() {
        if (happy) {
            std::cout << "meoww" << std::endl;
        } else {
            std::cout << "sssss" << std::endl;
        }
    }
    
    void Cat::makehappy() {
        happy = true;
    }
    
    void Cat::makesad() {
        happy = false;
    }
    
    } // namespace myNamespace
    

    ma​​in.cpp

    #include <iostream>
    #include "class.h"
    // using namespace std; // Again -Bad Practice
    
    int main() {
        using namespace myNamespace;    
    
        Cat jim;
        jim.makehappy();
        jim.speak();
    
        Cat george;
        george.makesad();
        george.speak();
    
        return 0;
    }
    

    试试这个看看你是否得到同样的编译器错误。这应该可以帮助您查看是否在多个空间中定义此函数。此外,通过删除 using namespace std; 并仅对 std:: 命名空间使用范围解析运算符,将有助于消除任何可能的问题和未来可能出现的问题。

    【讨论】:

    • @Georgez 嗯;尝试清洁您的解决方案。删除所有旧的目标文件。然后重新编译并重建。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-29
    • 1970-01-01
    • 2016-02-12
    • 2012-10-27
    相关资源
    最近更新 更多