淘客熙熙

主题:用g++编译的c++程序怎样引用外部类的static 变量? -- 金口玉言

共:💬26 🌺11 新:
分页树展主题 · 全看首页 上页
/ 2
下页 末页
  • 家园 用g++编译的c++程序怎样引用外部类的static 变量?

    用g++编译的c++程序怎样引用外部类的static 变量?

    不好意思,头一次用g++,在VC下可以编译的程序,在g++下却不行。

    主程序:

    #include <stdio.h>

    #include <SSS.h>

    int main()

    {

    printf("hello\n");

    printf("%d",SSS::T);

    }

    外部类的SSS.h文件

    #include <vector>

    class SSS

    {

    public:

    SSS(void);

    static float S,K,r,h,b,p;

    static int T;

    static void reset();

    public:

    ~SSS(void);

    };

    外部类SSS.cpp文件:

    #include "SSS.h"

    float SSS::K=100;

    float SSS::S=105;

    float SSS::b=-0.03;

    float SSS::r=0.03;

    float SSS::h=0.05;

    int SSS::T=3;

    float SSS::p=(h-r)/(h-b);

    SSS::SSS(void)

    {

    }

    void SSS::reset(void)

    {

    ::std::vector<int> v;

    v.reserve(10);

    v.push_back(100);

    }

    SSS::~SSS(void)

    {

    }

    编译的时候出错如下:

    stl@ubuntu:~/hhh$ g++ -o hh Hello.cpp SSS.cpp

    Hello.cpp:2:17: error: SSS.h: No such file or directory

    Hello.cpp: In function ‘int main()’:

    Hello.cpp:7: 错误: ‘SSS’ 未声明

    Hello.cpp:7: 错误: ‘T’ 在此作用域中尚未声明

    请各位大大指教怎么回事阿??非常感谢!!

    • 家园 如果不想改源代码,还有一个办法,

      把调用编译的命令行改为:

      g++ -o hh -I. Hello.cpp SSS.cpp

    • 家园 应该是有的

      我反正是apt-get install g++,一路安装成功就开始用了。Ubuntu真TM方便,其实g++这个名字我都是猜的,也许g++的真实名字或者全称不叫g++:) 大侠莫见笑。


      本帖一共被 1 帖 引用 (帖内工具实现)
    • 家园 g++ 还有汉化版?:)
    • 家园 来两个难点的,这问题一出来就被大家消灭了:)
    • 家园 提个议...

      以后能不能在这版多讨论一些类似的技术问题。目前的信息技术基本是信息技术“轶事”。

      也可能很多人更喜欢消遣,在工作外尽量忘记那些折磨人的东西,各取所需吧。

      • 家园 不可能讨论技术问题

        在西西河就不是干这个的,捎带着谈一下还行。

    • 家园 How Header Files Are Located

      C++ How to Program, 5th edition, page 98, Section 3.8.

      " How Header Files Are Located

      Notice that the name of the GradeBook.h header file in line 7 of Fig. 3.10 is enclosed in quotes (" ") rather than angle brackets (< >). Normally, a program's source-code files and user-defined header files are placed in the same directory. When the preprocessor encounters a header file name in quotes (e.g., "GradeBook.h"), the preprocessor attempts to locate the header file in the same directory as the file in which the #include directive appears. If the preprocessor cannot find the header file in that directory, it searches for it in the same location(s) as the C++ Standard Library header files. When the preprocessor encounters a header file name in angle brackets (e.g., <iostream>), it assumes that the header is part of the C++ Standard Library and does not look in the directory of the program that is being preprocessed. "

      "Error-Prevention Tip 3.3

      To ensure that the preprocessor can locate header files correctly, #include preprocessor directives should place the names of user-defined header files in quotes (e.g., "GradeBook.h") and place the names of C++ Standard Library header files in angle brackets (e.g., <iostream>)."

    • 家园 路径问题?

      VC的option那里设置了一大堆路径,g++需要自己设置吧

    • 家园 an introduction to gcc

      is a very good (and short) reference on GNU compiler gcc and g++. You can google to download it.

    • 家园 你include有问题。

      天边一只猴的看法应该是对的。

      #include <stdio.h>

      #include <SSS.h>

      stdio.h是系统带的,所以用<>,而SSS.h是你自己写的放在当前目录下吧,如果是这样,应该用“”

      #include <filename.h> 和 #include “filename.h” 有什么区别?(5 分)

      答:对于#include <filename.h> ,编译器从标准库路径开始搜索 filename.h

      对于#include “filename.h” ,编译器从用户的工作路径开始搜索 filename.h

      (引自林锐的《高质量C++编程指南》)

      • 家园 西河还是高人多啊

        昨天在一个linux的C++论坛上也提问了,都没有一个人理我。想了想,还是到河里来提问。西河确实学历高啊!而且你看天边那个猴子解问题那个自信的口气,还真是没见过。。。

        ps,以前就知道include "" 和<>有一个小小区别,但是没记住是什么区别。结果今天就碰上了,印象深刻啊。

        再次感谢,一人一朵花!

分页树展主题 · 全看首页 上页
/ 2
下页 末页


有趣有益,互惠互利;开阔视野,博采众长。
虚拟的网络,真实的人。天南地北客,相逢皆朋友

Copyright © cchere 西西河