五千年(敝帚自珍)

主题:用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’ 在此作用域中尚未声明

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

家园 没用过...

试试

gcc/g++ 命令有个 -include (外部文件)的选项;只是建议,没有任何把握。也没有环境测试。

家园 Compiler没找到SSS.h文件嘛。

你换成#include "SSS.h"试试。

家园 除楼下所说,另外可下面方法,

1.把sss.h的path 写入程序。

例.

#include “/home/wukw/cplus/h/sss.h”

家园 你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 "" 和<>有一个小小区别,但是没记住是什么区别。结果今天就碰上了,印象深刻啊。

再次感谢,一人一朵花!

家园 an introduction to gcc

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

家园 路径问题?

VC的option那里设置了一大堆路径,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>)."

家园 提个议...

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

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

家园 不可能讨论技术问题

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

家园 这个问题

在古早古早的著名的K&R版C Programming Language 就讲过了。

家园 这个区别我大学就知道了,虽然没用用过C/C++语言.

哪位大侠能给点经验?

本人大学学自控的,大学里C语言还学得不错,88分.但是大学毕业后用汇编工作,没有机会用C/C++,但是现在想自学一下C, 希望能尽量提高C的水平, 大家能否给点建议, 应该用什么教材,什么编译软件, 如何调试, 如何找资料....

没有经验, 还请大家不吝赐教.

家园 about C

The classical textbook on C programming language is Kernighan and Ritechie (K&R), The C Programming Language, 2nd edition. The official website is 外链出处.

For free download, try 外链出处. Click on the category: C.

The exercise problems are very good and worth of doing.

For the compiler, a very easy-to-use one is Bloodshed Dev C++ compiler 外链出处.

It's IDE and should be sufficient for doing the problems in K&R. If you need to compile and link several files together, GNU gcc is better. You can download it at 外链出处 .

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


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

Copyright © cchere 西西河