Natrium
Natrium

Reputation: 185

Why am i getting errors when changing a file from .c to .h?

I'm trying to make a header file for use in a program that plots vectors to a coordinate system and the problematic part of the code looks like this:

struct vector {
    float x, y;
};
struct vector vectoradd(struct vector V, struct vector W) {
    return (struct vector) {V.x + W.x, V.y + W.y};
}

This works flawlessly if I run it as a .c file but if I change the file extension to .h and add #pragma once, visual studio underlines this part of the code:

return (struct vector) {V.x + W.x, V.y + W.y};
//                     ^                   

This seems pretty bizarre to me but note that this is one of the first header files I have ever made.

Upvotes: 0

Views: 57

Answers (1)

Boris Lipschitz
Boris Lipschitz

Reputation: 1631

I don't think its moving your code from .c to .h is what causing the problem, because .h files are compiled along with the .c files that include them. Any chance the original file has been indeed a .c file, yet the new file that includes the .h is a .cpp?

If that's the case, can't you just use a temp variable instead of non-standard syntax? Also, i'd add inline as well, if i'd really want it in the .h and replicated everywhere that badly:

inline struct vector vectoradd(struct vector V, struct vector W) 
{
    struct vector tmp = {V.x + W.x, V.y + W.y};
    return tmp;
};

Upvotes: 1

Related Questions