In function int main

In function int main

Может кто-нибудь помочь мне разобраться с этой ошибкой

У меня есть два файла под исходными файлами в Visual Studio 2013 Express
main.cpp и Variables.cpp

ниже приведены коды

main.cpp

Variables.cpp

ошибка

Предупреждение 1
предупреждение C4305: «инициализация»: усечение с «double» до «float» c: users iifra Documents visual studio 2013 projects testproject001 testproject001 variables.cpp 11 1 TestProject001

Ошибка 2
ошибка C2084: функция ‘int main (void)’ уже имеет тело c: users iifra Documents visual studio 2013 projects testproject001 testproject001 main.cpp 6 1 TestProject001

Предупреждение 3
предупреждение C4305: «инициализация»: усечение с «double» до «float» c: users iifra Documents visual studio 2013 projects testproject001 testproject001 variables.cpp 11 1 TestProject001

Решение

Изменить название функции main() присутствует в Variables.cpp для любого другого имени.
Вы не можете использовать две функции main () в одном проекте, потому что ваша ОС находит основную функцию, присутствующую в вашем проекте, когда вы запускаете проект. И здесь ОС путает, какую основную функцию вызывать первой.

Другие решения

Это вопрос для начинающих. Два аспекта:

  • Вы можете иметь только 1 функцию «main», так как «main» является особенной (точка входа)
  • вы можете использовать несколько исходных файлов; используйте заголовок для объявлений и источник для определений

Компилятор будет обрабатывать их как два модуля перевода и создает два файла obj (то есть main.obj и variables.obj), а компоновщик объединит их вместе как один exe.

Вы используете Visual Studio. Поместите заголовочные файлы в папку заголовка, а файлы cpp — в исходную папку.

I am having problems calling my functions from my main program.
These functions HAVE to be in my class.
How do I access them from my int main()?

Here is an error I get:

what type do they need to be? pthread_mutex_t_create? or pthread_t_create?
what is the proper type?

6 Answers 6

Functions inside a class are called methods. You need to instantiate an object of that class to be able to use it’s methods:

EDIT:

By the way, pthread_create and pthread_join take a pthread_t* and not a mutex!

You can either declare those methods as static or use an object to make the calls:

You are calling class methods as just waitSemaphore without creating the object of myCountingSemaphoreUsingBinarySemaphore.

You should create the object first.

The two threads you create (via reader_writer() ) do nothing. main() just goes into the first do loop with no way of getting out.

Also, you seem to have confused mutex, semaphores, and condition variables. The function names makes it look like you’re trying to implement condition variables in your class. But you’re building it as just wrappers to mutex locks.

And finally, you are calling pthread_mutex_lock() et al. on a pthread_t when those functions are supposed to be called on a pthread_mutex_t .

There probably are other errors, but these are the ones that really jump out. Basically, you need to review multi-threaded programming, both in terms of how threads are created, and how they are synchronized.

Use your browser’s ‘Find. ‘ menu option to search through this page for a `unique’ word in your error message.

g++ can give some compiler/linker errors that don’t seem to make a whole lot of sense. What follows are some common errors that it gives and what will cause them.. For most errors, g++ will start the error message by telling you what function it has discovered the error in. Generally this will be something like: This is NOT the actual error message. It is just telling you which file and function it found the error in.

Disclaimer: The code given below isn’t necessarily the best way to code. It is only given to point out errors.

The list (numbers to the right of the code are line numbers. They aren’t actually in the code):

    Error: Pertinent Code: Cause:
    The insertion operator has been used instead of the extraction operator. Note: the ‘float &’ might be something else in your error.

Solution:
Replace the with >> .

Error: Pertinent Code: Cause:
The extraction operator has been used instead of the insertion operator. Note: the ‘char[7]’ might be something else in your error.

Solution:
Replace the >> with .

Error: Pertinent Code: Cause:
There is a missing semicolon. This is g++’s "catch-all" error message. If it can’t figure out what is wrong, it will give this message. It is telling you that it has found some error before the >. Generally this means that you are missing a semicolon on the line of code above that.

You might also get a parse error before an equals sign which is caused by the same problem. In fact, you can get parse errors before just about anything, and they often mean that the line of code above that is missing a semicolon.

Читайте также:  Унф дата запрета редактирования

Solution:
Put in the semicolon.

Error: Pertinent Code: Cause:
You aren’t using the fValue variable anywhere inside your code. g++ is warning you that this is a problem. Warnings don’t actually keep your code from compiling. It will still compile; however, warnings usually indicate a problem somewhere so you need to get rid of them.

Solution:
If you aren’t actually needing that variable, don’t declare it. Otherwise, make sure that you don’t have a typo anywhere in your code that is causing this error to show up. For instance, if you have used ‘fvalue’ somewhere in your code you will probably get two errors. One telling you that you are using ‘fvalue’ undeclared, and a warning telling you that you aren’t using ‘fValue’.

Error: Pertinent Code: Cause:
All of the error lines that list line 7 as the problem line are one error. That isn’t three different errors on line 7. It is telling you that you are trying to use the variable fvalue and you have not declared it yet. In addition, there is a warning that fValue is not being used.

Solution:
Make sure that the variable name given in the declaration exactly matches its use in your C++ code.

Error: Pertinent Code: Cause:
endl is only defined for use with cout .

Solution:
Get rid of the >> endl on the cin line. ONLY use endl with cout .

Error: Pertinent Code: Cause:
Note that the error is listed before the ‘In function `int main()’. This means that the error occured before the beginning of the main function. In this case, include was misspelled.

Solution:
Spell include properly.

Error: Pertinent Code: Cause:
A file with the name person.h does not exist in the current directory. This could be because the filename isn’t spelled correctly. Make sure that you have the capitalization the same. It is common to capitalize the first letter of header files that contain structures/classes. Also, the file might exist, but be in a different directory. It needs to be in the same directory.

Solution:
Make sure that there is a file in the same directory as the source file that has the name that you have inside the quotes on the include line.

Error: Pertinent Code: Cause:
All structures/classes need to have a semicolon after their definition (this is IN the header file). As soon as the compiler see the ‘int’ in ‘int main(void)’ it knows that whatever came before it (the structure definition in the header file) should have had a semicolon at the end.

Solution:
Put in the semicolon (in the header file, after the closing curly brace of the struct/class).

Error: Pertinent Code: Cause:
The colon at the end of Person. The other errors are ‘cascading errors’ that show up because of the first error.

Solution:
Change the colon to a semicolon.

Error: Pertinent Code: Cause:
On line 1, #ifndef is misspelled. So, the end of the if can’t find a beginning that it matches up with.

Solution:
Spell ifndef correctly.

Error: Pertinent Code: Cause:
ifndef is not spelled correctly. ifdef is the exact opposite of ifndef. ifdef will cause the contents of the header file to be ignored; hence, the Person structure will never be defined.

Solution:
Spell ifndef correctly.

Error: Pertinent Code: Cause:
You are missing a closing quote. g++ figured this out on line 9. However, it thinks that the problem started on line 6. It generally does a pretty good job. In this case, the closing quote after "Hello" is missing.

Solution:
Remember to always end literal strings with the closing quote.

Error: Pertinent Code: Cause:
In this case, the problem isn’t a missing semicolon on the previous line. This time, it is telling you that there is a parse error before the conio.h does not exist. Obviously, the error above could be replaced with whatever that you have inside the <> on the include line. That filename simply doesn’t exist.

Solution:
If it is a header file that you have written yourself, make sure that there is a file in the directory with that exact name (remember, we are case-sensitive). If it is a file like conio.h then you should be aware that Unix doesn’t have such a header file.

Error: Pertinent Code: Cause:
The cin object has been used incorrectly. When you use cin to read in a value it actually has to figure out what type of variable it is reading into (whether it be short, long, float, etc.) Other than the base types, cin doesn’t know how to read any other types of information. Hence, you can’t just tell cin to read in an entire structure or class and expect it to be able to do it. Note, you will get similar error messages if attempt to use cout to print out an entire structure/class.

Читайте также:  Re5dx9 exe неустранимая ошибка приложения

Solution:
You need to tell cin to read in each member of the struct/class individually (unless you overload the extraction operator). [ or, with cout you need to print out each member individually ]

This can be a tough error to track down. The thing that makes it so difficult is that fact that the important lines have usually scrolled off the screen before you can read them. The very first line of the error message will tell you the exact line that has the problem (line 8 in this case) but you often can’t see it on the screen. Here is one way to help find it: (that is the vertical bar (or ‘pipe’ as it is often called) followed by the ampersand, followed by the word ‘more’. Note: This works for the csh shell and the tcsh shell. If you don’t know what I am talking about, then this probably works for you.)
This sends the output of g++ (ie., the error messages) to a program ("more") that will only show you one screenful of messages at a time. You can just hit the space bar to page through the error messages, but be sure to note the line number that it has found the problem on before you start paging.

Error: Pertinent Code: Cause:
The preprocessor can’t find an end to the #if that it has. Hence, it is telling you that it is unterminated.

Solution:
Make sure that you have an end for the if. Proper usage is #endif . No spaces. The #endif should be the very last line of text in your header file.

Error: Pertinent Code: Cause:
On this one, the problem isn’t on line 8. Line 8 looks great. This message is telling you that are trying to access a member (m_strFirstName in this case) of a variable (personBoss) that is only a short integer. ie., personBoss is a short integer, and short integers don’t have "members".

Solution:
Look at the variable declaration for personBoss. Make sure that it is of a structure/class type. For Hungarian notation to work you have to make sure that you follow it completely. In this case, personBoss would need to be of type "Person" instead of type "short".

Error: Pertinent Code: Cause:
There is (most likely) a missing closing curly brace somewhere in your program. This is an interesting error because it is telling you that the error is on a line that doesn’t even (necessarily) exist in your program. The thing to remember is that the compiler completely ignores your indentation. So, in the above example it sees the Goodbye message as being inside the for . It sees the return as being inside the for . Then the for ends, and there isn’t anything left in the input and it is confused.

Solution:
Make sure that you have a matching closing curly brace for every opening brace. If you indent correctly this isn’t very hard to do. If you don’t indent correctly. you should.

Error: Pertinent Code: Cause:
Anytime that you get the the parse error, the compiler doesn’t think that you have the appropriate closing curly braces to match up with the open curly braces that you have listed. The above example is a tricky one. The problem isn’t the existance of the curly braces. They exist, as they should. However, there are TWO places where quotes are missing. At the end of the Hi on line 7, and the beginning of the Goodbye on line 9. So, the computer sees one big cout, where it is going to print Hi . Because the closing curly brace is inside the quotes it does not get interpretted by the compiler as being a curly brace.

Solution:
I found this one by commenting out the entire if using the C style comments. My code then looked like: When I tried to compile this, the compiler gave an error about unterminated string constants which let me track down the bug. If you have an error that you absolutely can’t find, try to comment out sections of code and recompile. If you still have the exact same error, you know that it is not inside the section of code that is commented out.

Читайте также:  Редактирование pdf файлов online

Error: Pertinent Code: Cause:
"A" is not a character. It is a string. C++ uses something called a pointer to access character strings. A single character is basically a little integer, and C++ won’t let you compare the two.

Solution:
If you want to denote a single character, use the single tick marks, as in ‘A’ .

Error: Pertinent Code: Cause:
This is a common error. The warning concerning line 5 is the important line. In C/C++ you don’t technically have to declare functions (with prototypes). If you don’t, C/C++ will implicitly declare the function for you. It will automatically set up a function that returns an int and can take any number of arguments. Since this is virtually NEVER what you want, you should always pay attention to this warning. In short, the error is that C++ can’t find your prototype for the function.

Solution:
This error can be caused from many different things. In the above example it is quite possible that you have forgotten a #include.
If you do have the proper #include for the function (make sure that it is the proper header file that has the prototype for the problem function), check to make sure that you have spelled the function name correctly. Remember that C++ is very picky about spelling. If you have the invokation ‘myFunction();’ and you have the prototype as ‘void myfunction();’ it won’t work.

Error: Pertinent Code: Cause:
This time, the compiler is happy. It compiles properly. It can see what ‘myFunction’ looks like, and it knows that it is being called properly in main.
The linker, however, is not happy. Any time that you get errors that start with /var/tmp/whatever.o: this means that the linker is giving you the error. It is telling you that myFunction(void) is undefined. It can’t find a function definition for myFunction that matches the way that you are invoking it.

Solution:
Often this is caused by not including all of the cpp files on the g++ line when you attempt to compile/link. If you do have all of the cpp files necessary, make sure that the prototype for ‘myFunction’ EXACTLY matches the first line of the function definition (except for the semicolon, of course). If there is anything different that could cause problems. For instance, you might have a function definition for myFunction, but it might start like: You will still get this error. The function definition needs to match the prototype.
If you are wanting to test your program before you actually write the entire code for ‘myFunction’ you will need to create a function stub for myFunction. It doesn’t have to do much, but it does have to exist. For the above example, a perfectly good function stub for myFunction might be:

Error: Pertinent Code: Cause:
myFunction() is being called incorrectly. According to the prototype myFunction should have a short integer inside the parenthesis (or not, in the second case). In this example the invokation doesn’t match that.

Solution:
Make sure that the function invokation matches the prototype. Note: It does NOT matter if the actual code for the function starts with: When the compiler tries to compile cppfile.cpp it only looks at the prototype, and the line in main does not match the prototype.

Error: Pertinent Code: Cause:
This is a linker error. It is seeing two function definitions for myFunction. This has been compiled with: This is the proper command line to use to compile. However the code isn’t correct. Remember what a #include does. It tells the preprocessor to take the code in that file and insert it. So, after the preprocessor gets done with cppfile.cpp, we are basically left with: This compiles fine. main can see what myFunction looks like, it is happy. This will compile into object code for two functions: main and myFunction.

Then, we compile myfunction.cpp. This will compile into object code for one function: myFunction. When the linker attempts to link everything together, it discovers that it has two sets of object code for myFunction.

Solution:
DON’T include cpp files! Never ever ever ever ever #include a cpp file. Only header files get #include’d.

Error: Pertinent Code: Cause:
There are some extra cpp files in the current directory that have a main function defined. So, you are getting the main() that you want, but you are also getting the other main() functions.

Solution:
If you are going to use the *.cpp method to compile make sure that you don’t have ANY other cpp files in the current directory.

Ссылка на основную публикацию
Adblock
detector