所以我的程序开头有一个头文件和2个.c文件.我去编译,我得到错误信息(大量的这些反复)
command_parser.c:74:6: error: static declaration of ‘read_args_file’ follows non-static declaration
command_parser.h:9:6: note: previous declaration of ‘read_args_file’ was here
Run Code Online (Sandbox Code Playgroud)
现在我不在我的程序中使用静态关键字ANYWHERE ...那么为什么GCC会认为我已经声明了一个静态函数?
下面是.h和.c文件中read_args_file声明的相关代码:
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out));
void read_args_file(char* file_name, char* out_file_name, int (*command_read)(char* command, FILE* out)) {
.....
}
Run Code Online (Sandbox Code Playgroud)
编辑:
整个.h文件是:
#ifndef COMMAND_PARSER_H_
#define COMMAND_PARSER_H_
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
/* line 8 follows: */
void switch_parsing(int argc, char* argv[], int (*command_read)(char* command, FILE* out), char* (*pr int_usage)()) {
void read_args_file(char* file_name, char* out_file_name, …Run Code Online (Sandbox Code Playgroud) public class A{
{
System.out.println("hi i am IIB");
}
public A(){
System.out.println("hi i am constructor");
}
public static void main(String... args){
A objA=new A();
}
}
Run Code Online (Sandbox Code Playgroud) 在C中它是一个错误
int x=5;
static int y=x; //error
Run Code Online (Sandbox Code Playgroud)
在C++中它有效为什么?
int x=5;
static int y=x; //valid
Run Code Online (Sandbox Code Playgroud) 我正在尝试用C ++编写基本的游戏代码。我有一个名为player的类,具有一个名为moveLeft()的函数;它会持续监视我要在单独线程中运行的“ a”获取。
以我认为的方式来称呼它。我现在知道这是因为它是一个非静态函数,但是我不确定现在如何执行此操作。我正在使用<thread>库。
#pragma once
class player
{
public:
player(); // Constructor
~player(); // Destructor
void moveLeft();
void moveRight();
void jump(double);
int getHealth();
double get_x_position();
double get_y_position();
double get_x_momentum();
double get_y_momentum();
bool getGrounded();
void setHealth(int);
void set_x_position(double);
void set_y_position(double);
void set_x_momentum(double);
void set_y_momentum(double);
void setGrounded(bool);
private:
int health;
bool grounded;
double x_position;
double y_position;
double x_momentum;
double y_momentum;
};
Run Code Online (Sandbox Code Playgroud)
void player::moveLeft()
{
while (true)
{
while (GetKeyState(97))
{
if (grounded)
{
x_momentum -= …Run Code Online (Sandbox Code Playgroud) 我试图在我的main createFile方法中从OpenFile类中调用我的方法,但我不断收到错误,说我不能从静态上下文中调用非静态变量.
我确实尝试OpenFile of = new OpenFile();在我的main方法中调用,但是这没有用,所以我现在声明OpenFile我的main方法上面工作正常,但是每次我尝试使用OpenFiles方法之一时我都会得到相同的错误.
我尝试过使用一些东西,static但这只会导致我的IDE显示错误的sym类型错误,我认为这是由引起其他错误的任何原因引起的.
这是createFile来自OpenFile:
public class OpenFile {
private Formatter file;
public void createFile() throws FileNotFoundException{
try{
file = new Formatter("test.txt");
} catch(Exception e) {
System.out.println("Error creating file.");
}
}
Run Code Online (Sandbox Code Playgroud)
这是我的主要方法:
OpenFile of = new OpenFile();
public static void main(String[] args) {
// TODO code application logic here
of.createFile();
intro();
createAndShowRibbon();
createAndShowNormalUI();
}
Run Code Online (Sandbox Code Playgroud)
它与Formatter有关吗?我以前从未使用过它.
谢谢.
我需要一些帮助来澄清静态和非静态变量。我的理解是静态变量在类的所有实例中具有相同的值。但是,假设我在同一个类中混合了静态和非静态变量。当我引用一个静态变量时,无论使用哪个实例,我都会得到相同的值?然而,当我引用一个非静态变量时,我将获得与该特定类关联的值?这似乎是一个内存管理噩梦。这真的是它的工作原理以及静态内存是如何处理的吗?是否在每个实例中创建了变量的多个副本,然后以某种方式同步,或者是在每个实例中创建的地址引用?在同一个类中混合使用静态和非静态变量是否有任何陷阱?TIA。
我正在尝试用天然卫星制造太阳系.目前我正在绘制一个相对于静态"太阳"类对象的行星,但我想让这个类使用另一个行星物体的位置并相对于该行星绘制.要做到这一点,我需要提取该行星对象的x和y位置.
这是我用于绘制行星的类的类构造函数.
Nebesko_Telo merkur = new Nebesko_Telo(Sun.x, Sun.y, 4, 12, 4.090909090909091,
1, 255, 255, 255);
// A field initializer cannot reference the non-static field, method,
// or property 'Form1.merkur'.
Nebesko_Telo venera = new Nebesko_Telo(merkur.x, merkur.y, 1, 23, 1.5, 1, 176, 108, 32);
Run Code Online (Sandbox Code Playgroud)
这是类构造函数.
public Nebesko_Telo(doubl _rel_tel_x, double _rel_tel_y, double _r, double _or,
double _Fi_mult, double _tilt_plant_nat, int _re, int_gr, int _bl) {
r = _r;
or = _or;
Fi_mult = _Fi_mult;
re = _re;
gr = _gr;
bl = _bl; …Run Code Online (Sandbox Code Playgroud) 我希望非静态块总是在创建对象时执行.但在下面的示例中,我调用了静态方法,但执行了非静态块.我没有创建任何对象,为什么非静态块执行?
class Example {
static void Mark() {
System.out.println("Mark method");
{
System.out.println("Hello");
}
}
}
public class StaticObject {
public static void main(String[] args) {
Example.Mark();
}
}
Run Code Online (Sandbox Code Playgroud)
结果:
Mark method
Hello
Run Code Online (Sandbox Code Playgroud) non-static ×8
static ×6
java ×4
c ×2
c++ ×2
class ×2
block ×1
c# ×1
constructor ×1
gcc ×1
initializer ×1
variables ×1