Conflicting Errors in Visual Studio (C++) - c++

I have a function "PrintHeader" for my project, defined in io.cpp. Even though io.h is included in my main file, I get the error
error C3861: 'PrintHeader': identifier not found.
When I copy the function for PrintHeader into my main file, I get the errors
error LNK2005: 'void _cdeci PrintHeader(void)" (?PrintHeader##YAXXZ) already defined in io.obj.
and
error LNK1169: one or more multiply defined symbols found.
I can understand the second error set, since I do have it defined twice, but I don't understand why it doesn't work when I just remove the duplicate definition. Any help is greatly appreciated.
Main file
#include "stdio.h"
#include <iostream>
#include "io.h"
void PrintHeader()
{
cout << endl;
cout << "Month\tPrincipal\t Interest\t Balance" << endl;
cout << "-----\t---------\t---------\t---------" << endl;
}
int main()
{
cout << "Hello World\n";
PrintHeader();
getchar();
return 0;
}
io.cpp
#include <iostream>
#include <iomanip>
#include "io.h"
void PrintHeader (void)
{
cout << endl;
cout << "Month\tPrincipal\t Interest\t Balance" << endl;
cout << "-----\t---------\t---------\t---------" << endl;
}
io.h
#ifndef __IO_H__
#define __IO_H__
#include <string>
using namespace std;
void PrintHeader (void);
#endif

You are most likely including the wrong file in main.cpp. You can make sure it is the right file by right clicking on the include "io.h" and choosing open file.

Related

Unresolved external symbol(LNK2019) [duplicate]

This question already has answers here:
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)" (?invoke_main##YAHXZ)
(20 answers)
Closed 4 years ago.
I am new to C++ . I am trying to develop a C++ application but there is one error that is keep bothering me.
Error LNK2019 unresolved external symbol _main referenced in function "int __cdecl invoke_main(void)"
I think I have referenced all of my functions in another file
This is the code of my cpp file:
#include "stdafx.h"
int console::main()
{
system("cls");
cout << "-----------ABC Homestay Management System-----------" << endl;
cout << "------------------------------------------------------------" << endl;
system("color 0f");
cout << "Please enter your choice" << endl;
cout << "1.Upload listings" << endl;
cout << "2.Find listings" << endl;
cout << "3.View listings" << endl;
cout << "4.Exit" << endl;
int choice;
cin >> choice;
switch (choice) {
case 1:
//renter();
break;
case 2:
//finder();
break;
case 3:
//listings();
break;
case 4:
exit(0);
break;
case 8:
//staff();//secret key -> 8 for staff
break;
}
system("pause");
main();
return 0;
}
void console::finder() {
system("cls");
cout << "test" << endl;
}
This is the "stdafx.h" header file that I have referenced in the cpp file :
#pragma once
#include "targetver.h"
#include <iomanip>
#include <ctime>
#include <time.h>
#include <windows.h>
#include <stdio.h>
#include <tchar.h>
#include <stdio.h>
#include <tchar.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <string>
#include <cstdio>
#include <cstdlib>
#include <regex>
#include <stdlib.h>
#include <string.h>
#include<algorithm>
#include<iterator>
#include <chrono>
#include <thread>
using namespace std;
class console {
public:
console() {}
~console() {}
int main();
void renter();
void finder();
void listings();
void staff();
};
Your program doesn't have a main function which is the entry point of a C++ program. The console::main() function cannot serve for this purpose, BTW you don't have any variable of type console at all in your program, therefore you never can call any of the methods of the console class. I think you should start reading your C++ text book from the beginning.
You want this:
...
int main()
{
console myconsole;
myconsole.main();
}
and BTW, this is fishy:
system("pause");
main(); // you probably want to remove this
return 0;
you probably want a loop here.
In C++, entry point of program is main function and it has to be outside of any class. In your code, you declared int main() inside console class.
All c++ programs must have an entry function called main that is not part of any class.
Correct routine would be a main function that creates your object that represents the bulk of your program and then run that's main function:
int main()
{
console myconsole;
myconsole.main();
}

"endl" causes "C1001" error

My code is a basic HelloWorld but fails to compile when I use cout<<endl.
I'm using Microsoft visual studio fresh download and created a console application for my first test project.
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
It generates a
"C1001" at line 1.
Replacing "endl" with ""\n"" works though.
You don't need the precompiled header #include <stdafx.h> so you can safely get rid of it. Also get rid of using namespace std; because it pollutes the global namespace. Try something like this. There's no reason it shouldn't work.
#include <string>
#include <iostream>
using std::string;
using std::cout;
using std::endl;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
return 0;
}
In Visual Studio you can disable use of the precompiled header in the project settings.
I do not see what the problem is. Both options compile and execute for me.
RexTester cppOnline
// Test1ConsoleApplication.cpp : Defines the entry point for the console application.
//
//#include "stdafx.h"
#include <string>
#include <iostream>
//#include <ostream>
using namespace std;
int main()
{
string s = "hello world!!";
cout << "lets see: " << s << endl;
cout << "lets see: " << s << "\n";
return 0;
}
So idk what was causing the error but it was fixed after pasting imports to the "stdafx.h" header file and then delete them...

C++ Headers & Undefined Reference

So I'm just getting to grips with C++ and progressing onto using header files. Thing is, I'm totally confused. I've read a documentation but none to make me understand it well enough.
I'm just making a silly 'game' which is interactive, it will probably be discarded and thought I could practice use on header files. This is my file structure:
terminal_test
├── core.cpp
└── game
├── game.cpp
└── game.h
Now, here is my core.cpp:
#include <iostream>
#include <stdio.h>
#include <unistd.h>
#include "game/game.h"
using namespace std;
void mainMenu();
void rootInterface() {
cout << "root#system:~# ";
}
int main() {
system("clear");
usleep(2000);
mainMenu();
return 0;
}
void mainMenu() {
int menuChoice = 0;
cout << "[1] - Start Game";
cout << "[2] - How To Play";
cout << endl;
rootInterface();
cin >> menuChoice;
if ( menuChoice == 1 ) {
startGame();
} else if ( menuChoice == 2 ) {
cout << "This worked.";
}
}
Everything else works fine but startGame(); under my menu choice. When I compile using g++ core.cpp game/game.cpp it bounces back with this error: undefined reference to startGame();. I firstly did some troubleshooting to see if it was properly finding game.h by changing the #include "game/game.h" to something like #include "game.h" without the directory listed inside and it gave me a game.h could not be found so I know it's recognising it, just not compiling at all.
Here is my game.h:
#ifndef GAME_H // Making sure not to include the header multiple times
#define GAME_H
#include "game.h"
void startGame();
#endif
game.cpp:
#include <iostream>
#include <stdio.h>
#include "game.h"
int main(int argc, char const *argv[]) {
void startGame() {
cout << "It worked.";
}
return 0;
}
My file structure isn't named properly either, I just threw it in because it was something to just get to grips with header files in C++.
So, here are my questions:
1) - What is this error specifically saying and what should I do to fix it?
2) - How do header files communicate and work with other files and is there clear documentation/guides out there that can help?
Local function definitions are not what you want here:
#include <iostream>
#include <stdio.h>
#include "game.h"
int main(int argc, char const *argv[]) {
// an attempt to define startGame() inside of main()
void startGame() {
cout << "It worked.";
}
return 0;
}
main is not needed in your game.cpp file. You should define startGame() outside of main, like this:
#include <iostream>
#include <stdio.h>
#include "game.h"
// definition of startGame
void startGame() {
cout << "It worked.";
}

Error while building project in Eclipse

When I compile a C++ project in eclipse it shows me errors saying that all functions in IO.cpp are already defined.
This is my code:
File: IO.cpp
#include <string>
#include <iostream>
using namespace std;
void print(string line) {
cout << line;
}
void println(string line) {
cout << line << endl;
}
void printError(string message, string error, string file) {
cout << "An error occurred!" << endl;
cout << "Message: "+ message << endl;
cout << "Error: "+ error << endl;
if(file != "") {
cout << "File/Method: "+ file << endl;
}
}
File: main.cpp
#include <string>
#include <iostream>
#include "IO.cpp"
using namespace std;
int main()
{
println("Hello world!");
}
You should remove the line below from main.cpp
#include "IO.cpp"
And add following lines afer using namespace std
void print(string line);
void println(string line);
void printError(string message, string error, string file);
If you include a cpp file again which is also present in your project source list (files to be compiled), your program will get multiple definitions for the same functions which is not allowed in C++. On the other hand, the alternative suggested here consists of declarations of function, which is allowed to present several times, but must present atleast once before first use.
Standard practice is to move the declarations in a header file (for ex: IO.h) and include this header file in both IO.cpp and main.cpp
Further readings:
Difference between declarations and definitions
You included module IO.cpp in module main.cpp
#include "IO.cpp"
So you have gotten the function definitions in two modules: IO.cpp and in main cpp
You should create a header file for example IO.h and place there all function declarations. Then you have to include this header file in IO.cpp and main.cpp
For example
IO.h
#include <string>
void print( std::string line);
void println( std::string line);
void printError( std::string message, std::string error, std::string file);
IO.cpp
#include <string>
#include <iostream>
#include <IO.h>
using namespace std;
void print(string line) {
cout << line;
}
void println(string line) {
cout << line << endl;
}
void printError(string message, string error, string file) {
cout << "An error occurred!" << endl;
cout << "Message: "+ message << endl;
cout << "Error: "+ error << endl;
if(file != "") {
cout << "File/Method: "+ file << endl;
}
}
main.cpp
#include <IO.h>
//...

Using Multiple Files in C++ for functions and classes

I am trying to figure out how to make a program consisting of separate files. I read this post:
Function Implementation in Separate File
But have not succeeded. I have 3 files: main.cpp, func.cpp, time.h and when I compile, I get this error message:
duplicate symbol getOpen(std::basic_ofstream<char, std::char_traits<char> >&)in:
/var/folders/kp/57zkm0tn1q7b7w0cs7tlf98c0000gn/T//cczaW1Px.o
/var/folders/kp/57zkm0tn1q7b7w0cs7tlf98c0000gn/T//ccvOCRgc.o
ld: 1 duplicate symbol for architecture x86_64
collect2: ld returned 1 exit status
I have no idea what this means. I'm basically just trying to open a file, write to it, and close it. I also just created an object and tested it. I know the problem is from func.cpp because when I remove that it works. Can someone please advise? Thank you.
I type this to compile: g++ main.cpp func.cpp.
This is my code:
time.h
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <fstream>
using namespace std;
class Time
{
private:
int seconds;
int minutes;
int hours;
public:
Time(int=0, int=0, int=0);
Time(long);
void showTime();
};
Time::Time(int sec, int min, int hour)
{
seconds = sec;
minutes = min;
hours = hour;
}
Time::Time(long sec)
{
hours = int(sec / 3600);
minutes = int((sec % 3600)/60);
seconds = int( (sec%60) );
}
void Time::showTime()
{
cout << setfill('0')
<< setw(2) << hours << ':'
<< setw(2) << minutes << ':'
<< setw(2) << seconds << endl;
}
func.cpp
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace std;
int getOpen(ofstream& fileOut)
{
string filename = "outfile.txt";
fileOut.open(filename.c_str());
if( fileOut.fail())
{
cout << "\nFailed to open file.\n";
exit(1);
}
else
return 0;
}
main.cpp
#include <iostream>
#include "time.h"
#include "func.cpp"
int main()
{
ofstream outFile;
Time t1;
t1.showTime();
getOpen(outFile);
outFile << "This is a test" << endl;
outFile.close();
return 0;
}
You included "func.cpp" into main.cpp, so you have a double declaration of getOpen.
You shouldn't #include "func.cpp" into main.cpp.
Well, it depends on how you compile your program. If you compile only main.cpp, then you should include func.cpp. But that's not a nice way to write a program, and I hope you are not going to do that.
What you might want to do is to compile main.cpp and func.cpp separately (using gcc -c) and then link the .o files. That would be perfectly ok if you don't include one .cpp file into another. But when you include func.cpp into main.cpp, both of the .o files define getOpen function. That causes the error.
So: just remove the #include in main.