【问题标题】:linux ubuntu c++ .h and .cpp filelinux ubuntu c++ .h 和 .cpp 文件
【发布时间】:2011-04-28 08:32:32
【问题描述】:

我有一个 my.h 文件:

#ifndef __MY__
#define __MY__

#include <string>
#include <time.h>

class S
{
    public: S();
    std::string myname;
};

#endif

我的.cpp

#include "my.h";

#include<string>
#include<iostream>

using namespace std;

S::S()
{
    // .. code
}

我想创建一个so文件。创建时没有错误。但是当我编译 .h 文件时,它说:字符串:没有这样的文件或目录。如果我 pus string.h 而不是 string 我有错误:在 my.h 中的 S(在 S 类)之前预期的 '=',',',';','asm'。 在 .cpp 文件中(如果我用 string.h 更改字符串)我在编译后出现错误:命名空间 std 中的字符串没有命名类型。我哪里错了?

【问题讨论】:

  • 你的编译命令行是什么?顺便说一句,__MY__ 这样的名称不允许在用户编写的代码中创建。
  • 另外最好去掉my.cpp第一行的分号
  • 我正在使用 geany 代码。我给构建
  • 在 test.cpp 我正在调用 .so 文件: void* handle = dlopen("/home/path/test.so", RTLD_NOW); if(!handle)0 { cout

标签: c++ linux ubuntu


【解决方案1】:

嗯,首先,您似乎来自 java,因为当您键入时:

class S
{
  public: S();
  std::string myname;
};

我猜你的意思是:

class S
{
  public:

    S();

  private:

    std::string myname;
};

.cpp 文件中,您输入了s 而不是S:请注意,C++ 对类名区分大小写。

另外,关于您的问题,我怀疑您当前使用的是C 编译器,而不是C++ 编译器。在不知道使用过的命令行的情况下,我不能多说。

【讨论】:

  • 感谢用户回答。为什么我在 cpp 中收到错误:命名空间 std 中的 my.h 字符串没有命名类型?
  • 我在 Geany 项目中工作。我刚刚从菜单中给出了构建。
  • 你的编译命令行是什么?我不知道 Geany 是什么。
  • with g++ my.cpp -o my i have errors: /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): relocation 0 has invalid符号索引 12 /usr/bin/ld: /usr/lib/debug/usr/lib/crt1.o(.debug_info): 重定位 1 的符号索引无效 13 /usr/bin/ld: /usr/lib/debug/ usr/lib/crt1.o(.debug_info): 重定位 2 的符号索引 2 无效
【解决方案2】:

试试这个

#ifndef MY_H
#define MY_H
#include <string>
#include <time.h>

class S
{
public: S();
std::string myname;
};

#endif



#include "my.h"
#include<string>
#include<iostream>

using namespace std;

S::S()
{
    //code
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-28
    • 2010-12-29
    相关资源
    最近更新 更多