I've created the below function(getHobby(Hobbies)) within a header file and when I'm calling this function in the header file itself, I'm getting an error:
conflicting types for 'getHobby'
HobbiesTest.h
enum Hobbies {
SKATING, SPORTS
};
char *getHobby(enum Hobbies hobbie) { <-- Compilation error "conflicing types for
'getHobby'
switch (hobbie) {
case SKATING:
return "SKATING";
case SPORTS:
return "SPORTS";
}
return "INVALIDOPTION";
}
void enumTest(){
printf("\nYour hobby is: %s",getHobby(SKATING));
}
And, when I'm calling the same function from the main file within some method, the same code is working fine.
Main.c
include <HobbiesTest.h>
int main(void) {
enumTest();
return 0;
}
void enumTest(){
printf("\nYour hobby is: %s",getHobby(SKATING));
}
Why I'm getting a compilation error in case of header file?
Put
enum Hobbies {
SKATING, SPORTING
};
At the start of header file. You using this enum before it is defined.
Related
I have added a library to my c project in codeVision AVR. when I want to use it's functions receive this error:
function 'function name' is declared but never defined.
here is my code:
#include "pid.h"
#include <mega32.h>
PidType _pid;
void main(void)
{
//some uC hardware initializing codes which are removed here to simplify code
PID_Compute(&_pid);
while (1)
{
// Place your code here
}
}
in pid.h:
.
.
bool PID_Compute(PidType* pid);
.
.
and pid.c:
#include "pid.h"
.
.
bool PID_Compute(PidType* pid) {
if (!pid->inAuto) {
return false;
}
FloatType input = pid->myInput;
FloatType error = pid->mySetpoint - input;
pid->ITerm += (pid->ki * error);
if (pid->ITerm > pid->outMax)
pid->ITerm = pid->outMax;
else if (pid->ITerm < pid->outMin)
pid->ITerm = pid->outMin;
FloatType dInput = (input - pid->lastInput);
FloatType output = pid->kp * error + pid->ITerm - pid->kd * dInput;
if (output > pid->outMax)
output = pid->outMax;
else if (output < pid->outMin)
output = pid->outMin;
pid->myOutput = output;
pid->lastInput = input;
return true;
}
the ERROR:
function 'PID_Compute' declared, but never defined.
Where is the problem?
EDIT:
to add the library to my project I placed the .c and .h library files in the same folder that my main project file is:
and then #include "pid.h" in my main file:
#include "pid.h"
#include <mega32.h>
// Declare your global variables here
PidType _pid;
void main(void)
{
.
.
my error and warnings:
EDIT2:
I simplified the code and now can show you the entire code:
main code:
#include "pid.h"
PidType _pid;
void main(void)
{
PID_Compute(&_pid);
while (1)
{
}
}
pid.h:
#ifndef PID_H
#define PID_H
#include <stdbool.h>
typedef struct {
int i;
} PidType;
bool PID_Compute(PidType* pid);
#endif
pid.c:
#include "pid.h"
bool PID_Compute(PidType* pid) {
pid->i = 2;
return true;
}
thank you every body.
As you said, the pid.c was not added to the project.
for those who may face the same problem:
in codeVision AVR we have to add .c file to project from project->configure->files->input files->add
I addec .c file to project and the error went away.
From the screenshot with the tree view of your files it is clear that the file "pid.c" is not part of the project.
Move it into your project. Then it should build without that linker error.
This does not mean the location in the file system. I reference the "virtual" view of the IDE on your project.
Problem: I want generate function prototype if it doesn't exists, in the C file, I have done this, but in case if the prototype already exists in a header file it doesn't feel it, BTW I used CDT IIndex
//C Code
#include "test.h" //where it have bar prototype
void bar(void)
{
//function body
}
//Obtaining the AST [JAVA CDT CHECKER]
ITranslationUnit tu = (ITranslationUnit) CDTUITools.getEditorInputCElement(editor.getEditorInput());
String cFilePath = tu.getResource().getFullPath().toString();
ICProject[] allProjects = CoreModel.getDefault().getCModel().getCProjects();
IIndex **index** = CCorePlugin.getIndexManager().getIndex(allProjects);
IASTTranslationUnit ast = null;
try {
index.acquireReadLock(); // we need a read-lock on the index
ast = tu.getAST(index, ITranslationUnit.AST_SKIP_INDEXED_HEADERS);
} finally {
index.releaseReadLock();
ast = null; // don't use the ast after releasing the read-lock
}
//In the AST visitor class
/**
* extract prototype and store them into 'prototypes' hash set and then rewrite the AST.
*/
#Override
protected int visit(IASTSimpleDeclaration simpleDeclaration) {
IASTDeclarator[] declarators = simpleDeclaration.getDeclarators();
boolean isProtoFuncDeclaration = false;
for (IASTDeclarator declarator: declarators) {
if( declarator instanceof IASTFunctionDeclarator)
{
isProtoFuncDeclaration = true;
}
}
if(isProtoFuncDeclaration)
{
prototypes.add(simpleDeclaration);
}
return super.visit(simpleDeclaration);
}
The output of the new C Code
#include "test.h" //where it have bar prototype
void bar(void) <=== I shouldn't be inserted as it is already in the header
void bar(void)
{
//function body
}
I would suggest the following:
Traverse the AST to find the IASTFunctionDefinition for which you maybe want to add a prototype.
Use getDeclarator().getName() to get the function name,
Use IASTName.resolveBinding() to get the function binding.
Use IIndex.findDeclarations(IBinding) to find all declarations and definitions of the function.
From the resulting IIndexName[], filter out the definition itself (check IName.isDefinition()).
Further filter the list to declarations that appear in included header files (since findDeclarations() will return declarations in the entire project). You can do this by calling IASTTranslationUnit.getIndexFileSet() on your AST, and checking IIndexFileSet.contains(name.getFile()) for every IIndexName.
If after this filtering, the list is empty, you know you need to add a prototype.
EDIT: This question is to be edited, please stop reading. Don't waste your time! Thank you
I’m doing High School Turbo C++. I tried making a header file containing a function to search a binary file.
My Headerfile program is: alpha.h
#ifndef ALPHA_H
#define ALPHA_H
#if !defined __FSTREAM_H
#include<fstream.h>
#endif
#if !defined __PROCESS_H
#include<process.h>
#endif
void searchclass(char* & buf, int, char *);
#endif
According to some research that I did on the internet, I found out that the definitions will go in a separate program not in the main header file. So this is that program: ALPHA.CPP
#include<process.h>
#include<fstream.h>
#include"alpha.h"
//All the Definations of the alpha.h header file go here
void searchclass(char* & buf, int s_var, char * file)
{
ifstream fin;
fin.open(file, ios::binary);
if(!fin)
{
cout<<"Error 404: File not found";
exit(-1);
}
while(!fin.read((char*)buf, sizeof(buf)))
if(buf.getint()==s_var)
cout<<"\n\nRecord Found!";
buf.show();
fin.close();
}
Keep in mind that I'm trying to write a function that can help me search a random binary file storing records in form of classes for some specific int variable. So it should be able to take in an object of any class and perform a search in it.
This is the program I wrote to check my header file. A_TEST.CPP
#include<iostream.h>
#include<conio.h>
#include<fstream.h>
#include"alpha.h"
#include<string.h>
class stu
{ int rn;
char name[20];
public:
void show() //Display Function
{
cout<<"\n\tStudent Details:";
cout<<"\nName: "<<name;
cout<<"\nRoll No.: "<<rn;
}
stu() //constructor
{
rn = 6;
strcpy(name,"Random Name");
}
int getint() //return function
{
return rn;
}
};
char* returnfile()
{ char file[10];
strcpy(file,"file.dat");
return file;
}
void main()
{
clrscr();
int search_var=6;
stu S1;
char file[10];
strcpy(file, "test.dat");
ofstream fout;
fout.open(file, ios::binary);
fout.write((char*)&S1, sizeof(S1));
fout.close();
searchclass((char*)& S1, search_var, file);
getch();
}
On compiling A_TEST.CPP (above program), I get the warning:
Warning A_TEST.CPP 45: Temporary used for parameter 'buf' in call to
'searchclass(char * &,int,char *)'
On linking, it gives me this error:
Linking A_TEST.EXE
Linker Error: Undefined symbol searchclass(char nearnear&,int,char
near) in module A_TEST.CPP
I don't think that the ALPHA.CPP file is getting linked with the alpha.h file, and if I compile ALPHA.CPP file it gives me the following errors:
Error ALPHA.CPP 17: Structure required on left side of . or .*
Error ALPHA.CPP 19: Structure required on left side of . or .*
Warning ALPHA.CPP 21: Parameter 's_var' is never used
In ALPHA.CPP 17 and 19, you can't write code like that, because int type hasn't getint() or show() methods. If you want to check int in the buffer, you should convert pointer into "int*" first, try below code:
int count = fin.read((char*)buf, sizeof(buf));
for (int i = 0; i<(count-4); ++i) {
if (*(int *)(buf + i) == s_var) {
cout << "\n\nRecord Found!";
break;
}
}
I'm trying to create my own functions in C, and then use #include with a header file. I know how to make the header file, and I've written the .c function. However, when I try to compile the .c, I get an error that says '[Linker error]undefined reference to 'WinMain#16'' and it fails to compile.
Then, if I try to use it in a program, it says '[Warning]no newline at end of file' and then '[Linker error]undefined reference to validf(int, int, int)'.
Can anyone help?
Function Code:
int validf(int current,int max, int zero)
{
if(zero==1)
{
if(current>max || current<0)
{
printf("Invalid Input");
return 0;
}
else
{
return 1;
}
}
else if(zero==0)
{
if(current>max || current<=0)
{
printf("Invalid Input");
return 0;
}
else
{
return 1;
}
}
else
{
printf("Invalid parameters");
return -1;
}
}
Main Code:
#include<stdio.h>
#include<stdlib.h>
#include "validf.h"
int main()
{
int valid=0;
valid=validf(4,5,0);
printf("%d",valid);
system("\npause");
return 0;
}
Header Code:
#ifndef VALIDF_H_
#define VALIDF_H_
int validf(int current,int max,int zero);
#endif
Your program consists of 3 files:
validf.h header file for validf. Contains declaration of validf function
validf.c code file for validf. Contains definition of validf function.
main.c contains the main function. You may have chosen another name for this file.
In your IDE, you should create a project that consists of these files.
You also need to configure the name of the resulting program. I am not familiar with that particular IDE, but it is usually done under Project->Settings->Compile or Build or Link.
This will make your IDE compile the two .c files and then link them into a single program.
If you dont create a project it is probable that the IDE treats each .c file as a different program, which will cause the errors you mention.
So I've tried from another post to make this code:
while(fscanf(orderFile," %49[^;];%d; %49[^\n]",fileName,&seconds,timeValue) == 3)
{
count++;
if(count == linha)
{
fprintf(tempFile,"%s;%d;%s\r\n",orderNameFile,orderSecondsFile,orderTimeFile);
}
else
{
fprintf(tempFile,"%s;%d;%s\r\n",fileName,seconds,timeValue);
}
}
fclose(tempFile);
fclose(orderFile);
remove("order.txt");
rename("temp.txt","order.txt");
I also have included the stdio.h lib
#include <stdio.h>
Yet when I run this code, it gives an error on the remove function, saying:
error: called object 'remove' is not a function or function pointer
I've tried to create a char name[] = "order.txt"; and use it inside the remove(); instead but didn't work as well, also already created an int variable like int x; x = remove("order.txt"); and it didn't work.
Any ideas?
You have a variable named remove somewhere in your code, rename it.