Using Structs -- Odd Issue - c++

Been awhile since I've used structs in C++.
Any idea why this isn't working? My compiler is complaining about DataStruct not being a recognized type but Intellisense in VC++ is still able to see the data members inside the struct so the syntax is ok...
Frustating. xD
struct DataStruct
{
int first;
};
int main(int argc, char **argv)
{
DataStruct test;
//test.first = 1;
}

Are you sure you are compiling the file as C++? If you compile it as C (i.e. if the file has a .c rather than a .cpp extension), you will have problems.

You are compiling as C code. C requires you to refer to it using the "Struct" keyword or typedef it. C++ does not.

You need to use struct DataStruct to refer to the struct.
Alternatively, you can typedef it as DataStruct if don't want to use the "struct" everywhere.

Related

From C++ how to execute a method or access struct which is in C?

I need to pass string value from C++ to C library.
How do you call CppCallToC or from .cpp how can i access the struct rtmp_stream *stream; ?
C++ file: UI/window-basic-main.cpp:
void OBSBasic::Load(const char *file) {
//From C++ how to go to the C method?
CppCallToC();
}
C file: plugin/obs-outputs/rtmp-stream.c:
static void CppCallToC() {
//https://github.com/jp9000/obs-studio/blob/master/libobs/util/dstr.h
//struct dstr path, key;
struct rtmp_stream *stream;
//dstr_printf(&stream->path, obs_path);
//dstr_printf(&stream->key, obs_key);
}
Your problem is not a C vs C++ issue. All functions in rtmp-stream.c are declared static, which means they have internal linkage and cannot be called from a different compilation unit. You can only access those functions indirectly via the rtmp_output_info struct defined at the end of the file and declared in obs-outputs.c, and even that declaration is only available within that file.
I don't know anything about OBS Studio, so I can't help you any more than that, I'm afraid. You'll have to track down what happens in obs_register_output(&rtmp_output_info).

a value of type "Struct*" cannot be assigned to an entity of type "Struct*"

I've a function which returns a struct type pointer. What I want is pFacialFeatures to point to the same address as returned pointer.
struct Features
{
CvRect* face_;
CvRect* nose_;
CvRect* eyesPair_;
CvRect* rightEye_;
CvRect* leftEye_;
CvRect* mouth_;
};
Features* Detect()
{
Features* facialFeatures = (Features*) malloc(sizeof(Features));
return facialFeatures;
}
int main(int argc, char* argv[])
{
Features* pFacialFeatures;
pFacialFeatures = Detect();
}
It gives me the error:
IntelliSense: a value of type "Features *" cannot be assigned to an entity of type "Features *"
Note: Maybe you may think this question is same with this one. In that question there's a problem with declaring struct. I declared struct truely.
You have somehow informed Visual Studio that this is a C source file instead of a C++ source file - perhaps by naming the file "something.c" or by placing it in a header file and then including it from a ".h" file or by having a dangling "extern C" or by having somehow set the properties for the file or project to "Compile as C". If you're using Linux/MacOS, you've probably done it by using the C compiler instead of the C++ compiler, e.g. by typing "gcc foo.cpp" instead of "g++ foo.cpp"
The C language syntax for a struct declaration is different than that in C++.
The C++ statement
struct Foo {}; // C++
is equivalent to this in C:
typename struct tagFoo {} Foo; // C
So the following code would work in C++ but fail in C:
struct Foo {};
Foo* f = (Foo*)malloc(sizeof(Foo));
A quick way to change this to check for C++ is to replace:
Features* facialFeatures = (Features*) malloc(sizeof(Features));
with
Features* facialFeatures = new Features;
If you're compiling in C mode, you'll get a compiler error about new. It's a keyword in C++ but not in C.
The way to write your line in C is
struct Features* facialFeatures = malloc(sizeof* facialFeatures);
I belive you need to put struct before the declaration of the type:
struct Features* facialFeatures = (struct Features *)malloc(sizeof(struct Features));

creating c++ object in c file

I have a query for creating c++ object inside c file.
I have the sample code below. When trying to import the CPlusHeader it throws an error which i could not understand.
The error is iostream' file not found as one of the error. How could i resolve this issue.
Regards,
Lenin
CPlusFile.h
include iostream
include string
using namespace std;
class CPlusFile {
int data;
public:
CPlusFile();
int getData();
};
CPlusFile.cpp
CPlusFIle::CPlusFIle() {
data = 10;
}
int CPlusFile::getData() {
return data;
}
CFile.h
int doSomething();
CFile.c
include "CFile.h"
include "CPlusFile.h"
int doSomething() {
CPlusFile object;
}
It strongly depends on what you call a "C file". Previous answer assumed that you meant a file with a .c suffix. I assume here that you mean a file that shall be compiled with a C compiler.
If my assumption is valid, then the answer is simple: You cannot instantiate C++ classes in a C file. What you can do, though, is call C++ static methods from the C code. Please refer, for example, to In C++ source, what is the effect of extern "C"? to see how to do this.
First of all, it is
#include <iostream>
#include <string>
and not
include iostream
include string
Second, if CFile.c is compiled as C, then this will not work. The C compiler will not understand the class keyword and you cannot create an instance of a class in C.
iostream is a C++ header, and isn't available if you're compiling using a C compiler. You can write C++ code in a .c file, you just need to use the right compiler.
You can use this in your C++ header file to check wether you are going to include it from C or C++ code:
#ifdef __cplusplus
The includes iostream and others, as well as using class, are only available for C++ code.
But if you want to use the CPlusFile class, which is a C++ class, you can only do that in C++ code. Best is to rename your CFile.c to CFile.cpp.
Yes, it is possible to call C++ object inside the C file. Here I performed a scenario and
it's working fine for me.
CPlusFile.h
#include<iostream>
#include<string>
using namespace std;
class cplus{
int data;
public:
cplus();
int getdata();
};
CPlusFile.cpp
#include "cplusfile.h"
cplus::cplus(){ data =10; }
int cplus::getdata(){ return data; }
CFile.h
#include "cplusfile.h"
#include<stdio.h>
int dosomething();
CFile.c
#include "cfile.h"
int dosomething(){
cplus c;
printf("%d",c.getdata());
}
int main() {
dosomething();
return 0;
}
And compile this by g++ CFile.c CPlusFile.cpp and it works fine.

Extern unnamed struct object definition

I got a global object of type "unnamed-struct" and i'm trying to define it. I don't want to pollute my global namespace with such useless type (it will be used only once).
Global.h
extern struct {
int x;
} A;
Is there any correct way to define such object?
I was trying this:
Global.cpp
struct {
int x;
} A = { 0 };
But VS2012 throws "error C2371: 'A' : redefinition; different basic types". Thanks.
One possible solution: create another file Global_A.cpp that does not include Global.h, and define A there. By the equivalent-definition rule this will be valid, as long as the anonymous struct definitions are equivalent.
This is still a bad idea, and most compilers will warn about it e.g. (gcc): warning: non-local variable `<anonymous struct> A' uses anonymous type.
There is no way to do just this simply because it would be error prone: some time in the future someone (probably even you) may want to modify this structure's definition and he might forget to do this in the header and source files accordingly. So you will have to invent a name for this structure and use its name the source file and leave its definition up to the header.
I don't know if this helps you, and this my first post... So take it easy.
I just ran into this issue and what I did was I added a function that manipulates the struct to the file that contained the anonymous struct. That way I can call that function from any file in my project, and it manipulates the values in the struct for me.
Here is an example:
header.c has this anonymous struct:
struct
{
char line1[80];
char line2[80];
char line3[80];
} header;
I want to manipulate those values in "interface.c" because I am making a command line interface in another file. My first instinct was to use an extern, but it seems like adding the following function to header.c is just as good or better (Some people discourage the use of externs when avoidable).
void changeHeaders(char *one, char *two, char *three);
void changeHeaders(char *one, char *two, char *three)
{
strcpy(header.line1, one);
printf("\nHeader 1: %s", header.line1);
strcpy(header.line2, two);
printf("\nHeader 2: %s", header.line2);
strcpy(header.line3, three);
printf("\nHeader 3: %s", header.line3);
}
Now as long as I include the prototype for that function I can manipulate those struct variables from any file by using that function. Hope that helps someone.

C++ - struct errors

Following an example at: http://www.learncpp.com/cpp-tutorial/47-structs/ relating to structs, and when I tried to compile this program:
#include <iostream>
void PrintInformation(Employee sEmployee)
{
std::cout<<"ID: "<<sEmployee.nID<<std::endl;
std::cout<<"Age: "<<sEmployee.nAge<<std::endl;
std::cout<<"Wage: "<<sEmployee.fWage<<std::endl;
}
struct Employee {int nID;int nAge;float fWage;};
int main()
{
Employee abc;
abc.nID=123;
abc.nAge=27;
abc.fWage=400;
// print abc's information
PrintInformation(abc);
return 0;
}
I get the following:
Why is that?
Thanks.
You need to declare the struct before the function that attempts to use it.
C (and by extension, C++) were designed for "single-pass" compilation. Therefore, everything must be available to the compiler by the time it's required.