我正在浏览其他人的代码并遇到以下语法:
typedef struct abc {
abc() : member(0){}
unsigned int member
}
Run Code Online (Sandbox Code Playgroud)
它似乎是一个带有成员变量和构造函数的类,除了它被声明为struct.我这里有两个问题.
非常感谢提前.
PS:我如何格式化代码?
当我在Visual C++中尝试这个时,为什么会出现错误?
struct S { };
typedef S *S;
Run Code Online (Sandbox Code Playgroud)
不是C++ 会给你typedef一个以前只被声明为class或者名字的名字struct吗?
或者我误解了发生了什么?
我有一个指向外部头文件中定义的 Map 类型结构的指针:
typedef struct {
char *squares; //!< A pointer to a block of memory to hold the map.
int width; //!< The width of the map pointed to by squares.
int height; //!< The height of the map pointed to by squares.
} Map;
Run Code Online (Sandbox Code Playgroud)
指针初始化如下:
struct Map *map_ptr;
map_ptr = create_map(*w_ptr, *h_ptr);
// create_map returns Map*, w_ptr and h_ptr are pointers to height and width fields for a map/maze.
Run Code Online (Sandbox Code Playgroud)
如何打印在 create_map 中创建的 Map 结构中存储的宽度和高度值?create_map 保存在外部文件中,它传递回 main 的唯一变量是指向映射的指针。
编译时出现以下错误(“错误:取消引用指向不完整类型的指针”) …
我的印象是,您可以在一个文件中定义类的成员函数,然后在另一个文件中使用这些函数,只要这两个文件都被编译并发送到链接器即可.但是,如果我使用g ++(4.6.4),这样做会给我一个未定义的引用错误.有趣的是,使用intel编译器(icpc 11.0)不会出错,一切正常.是否有一些我可以用g ++设置的标志来使这个工作,或者是英特尔编译器让我逃避我不应该做的事情?以下是一些可以重现我的问题的代码:
class.h:
#ifndef _H
#define _H
typedef class
{
public:
int a;
int b;
void set(int x, int y);
int add(void);
} Test;
#endif
Run Code Online (Sandbox Code Playgroud)
class.cpp:
#include "class.h"
void Test::set(int x, int y)
{
a = x;
b = y;
}
int Test::add(void)
{
return a+b;
}
Run Code Online (Sandbox Code Playgroud)
main.cpp中:
#include <cstdio>
#include "class.h"
int main(void)
{
Test n;
n.set(3, 4);
printf("%d\n", n.add());
return 0;
}
Run Code Online (Sandbox Code Playgroud)
为了编译,我做:
$ g++ class.cpp main.cpp -o test
/tmp/ccRxOI40.o: In function `main':
main.cpp:(.text+0x1a): undefined reference …Run Code Online (Sandbox Code Playgroud) 可能的重复:
为什么我们要在 C 中如此频繁地对结构进行 typedef?
C++ 中“struct”和“typedef struct”的区别?
以下类型声明有什么区别?
struct Person
{
int age;
};
typedef struct
{
int age;
}Person;
Run Code Online (Sandbox Code Playgroud)
我明白那个
struct
{
int age;
}Person;
Run Code Online (Sandbox Code Playgroud)
创建一个名为 person 的未命名结构的实例,其中
struct Person
{
int age;
};
Run Code Online (Sandbox Code Playgroud)
声明了一个名为 person 的类型,但不是一个实例。但我仍然不明白 typedef 的作用。
在下面的程序中,我尝试将结构传递给函数.但我得到错误,我不明白为什么.我在这个程序中犯了什么错误?
我gcc用来编译这个c程序.
#include <stdio.h>
struct tester {
int x;
int *ptr;
};
void function(tester t);
int main() {
tester t;
t.x = 10;
t.ptr = & t.x;
function(t);
}
void function(tester t) {
printf("%d\n%p\n",t.x,t.ptr);
}
Run Code Online (Sandbox Code Playgroud)
错误:
gcc tester.c -o tester
tester.c:8:15: error: unknown type name ‘tester’
tester.c: In function ‘main’:
tester.c:12:2: error: unknown type name ‘tester’
tester.c:13:3: error: request for member ‘x’ in something not a structure or union
tester.c:14:3: error: request for member ‘ptr’ in …Run Code Online (Sandbox Code Playgroud) 这是我的代码:
#ifndef PROBLEM3_H
#define PROBLEM3_H
#include <string>
struct stackItem {
// arbritary name do define item
string name;
};
class Hw3Stack {
public:
// Default constuctor that prompts the user for the stack
// capacity, initalizes the stack with the capacity, and the sets
// the size of zero.
Hw3Stack();
// Copy constructor that takes a current stack and copies the
// current size, capacity, and stackItems into the current object.
Hw3Stack(Hw3Stack& stack);
// Destructor that deletes the stack
~Hw3Stack(); …Run Code Online (Sandbox Code Playgroud) 我有一个看起来像这样的结构:
typedef struct {
int a;
//other fields
string s1;
string s2;
} strMyStruct;
Run Code Online (Sandbox Code Playgroud)
在下面的代码中,append()方法将崩溃,因为x1.s1未构造字符串对象.
strMyStruct x1;
memset(&x1, 0, sizeof(x1));
x1.a = 100
x1.s1.append("....");
Run Code Online (Sandbox Code Playgroud)
我可以做新的放置来解决它new (x1.s1) string;,但它太麻烦了,我没有完全控制结构strMyStruct:有人可能会添加另一个字符串作为一个字段到这个结构并开始使用它并遇到崩溃问题,直到他记得做新的招数.
真的希望字符串类可以在初始化完成后正确处理它memset(&x1, 0, sizeof(x1));.这有诀窍吗?
我在一个电机程序(我必须控制多个电机,这就是我使用结构的原因)和我的arduino MEGA一起工作.当我在驱动功能中使用它作为参数时,我不明白为什么MOTOR超出范围:
typedef struct motor
{
int EN;
/*some more ints*/
}MOTOR;
MOTOR mot1;
MOTOR mot2; /*this works with no compile error*/
int drive (MOTOR*) /*here i have compile error out of scope, neither with or without pointer*/
{
return 1;
}
void setup()
{}
void loop()
{}
sketch_jul25a:2: error: 'MOTOR' was not declared in this scope
sketch_jul25a:2: error: expected primary-expression before ')' token
sketch_jul25a.ino: In function 'int drive(MOTOR*)':
sketch_jul25a:9: error: 'int drive(MOTOR*)' redeclared as different kind of symbol
sketch_jul25a:2: …Run Code Online (Sandbox Code Playgroud) 我试图创建一个包含指向其类型的指针的结构:
#include <stdio.h>
struct test{
test* t;
};
int main(){
return 0;
}
Run Code Online (Sandbox Code Playgroud)
使用gcc编译时,此代码产生了一个错误:
:5:2:错误:未知类型名称'test'
但是在使用g ++进行编译时,它就可以了.
所以我想要了解的是:
1)是什么导致这种差异?我认为如果gcc使用一次通过编译而g ++使用可以解释它的multipass,但据我所知,这不是真的.
2)如何避免此错误并使用指向它自己类型的指针定义结构?(除非没有其他选择,否则我不想使用void*或使用cast)