Optimize source structure for C++ projects - c++

I would like to create a little project, which is divided in more then one file.
main.cpp:
#include <cstdlib>
#include <iostream>
#include sc_hpp
using namespace std;
int main(int argc, char *argv[])
{
add(3,4);
system("PAUSE");
return EXIT_SUCCESS;
}
sc.hpp:
#ifndef "sc.hpp"
#define sc_hpp
int add(int a, int b);
#endif
function.cpp:
#include "sc.hpp"
int add(int a, int b)
{
return(a + b);
}
But it doesn't work.
ERROR:
`add' undeclared (first use this function)
First time I'm trying make programme in more then one file, so I think the problem must be easy to solve.

You have two obvious mistakes:
In your main:
#include <cstdlib>
#include <iostream>
// the included header file needs to be enclosed in " "
// and it needs a suffix, i.e.: `.h`
#include "sc_hpp.h"
using namespace std;
int main(int argc, char *argv[])
{
}
In sc.hpp:
// the include guards doesn't have to be enclosed in " "
// the suffix's dot:'.' is replaced with underscore: '_'
// header name in uppercase letters
#ifndef SC_HPP_H
#define SC_HPP_H
int add(int a, int b);
// included .cpp files with function implementation here
#include "sc.hpp"
#endif
More on how to organize code files, here.
In general the preprocessor directive #include expands the code contained in the file that follows it, so your code in main looks like this:
#include <cstdlib>
#include <iostream>
// #include "sc_hpp.h" replaced with
int add(int a, int b);
// the #include "sc.cpp" nested within the "sc_hpp.h" is replaced with
int add(int a, int b)
{
return(a + b);
}
using namespace std;
int main(int argc, char *argv[])
{
}

Related

How can I write a function that returns to array inside the header?

I am trying to create a function that returns in main.cpp in the header and .cpp file and run it in the main function.
This process I do works on main.
#include <iostream>
#include <sstream>
#include "Cards.h"
using namespace std;
//this function returns array
int *function1(){
int a=12;
int b=13;
int c=14;
static int list[3]={a,b,c};
return list;
}
int main(int argc, const char * argv[]) {
int *list;
list=function1();
cout<<list[1]<<endl;
return 0;
}
However, I cannot do these in a header and a separate cpp file.
I have a Cards header
#ifndef Cards_H
#define Cards_H
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
class Cards{
public:
char suit; //A,H,D,C,S. A is empty card
int number; //00-13
int visibilty;//0 - 1. O invisible 1 is visible
int * function2();
};
#endif
This is the class cpp file
#include "Cards.h"
using namespace std;
//function
int Cards:: function2(){
int a=12;
int b=13;
int c=14;
int list[3]={a,b,c};
return list; // error code Cannot initialize return object of type 'int Cards::*' with an lvalue of type 'int [3]'
}
How do I fix this problem and run it in main?
As pointed out in the comments, there is already a SO thread
Return array in a function
which handles your issue.
If your really want to use C arrays then your program shall look like:
Cards_CStyle.h:
#ifndef Cards_CStyle_H
#define Cards_CStyle_H
using namespace std;
class Cards {
public:
int* function2(int arr[]);
};
#endif
Cards_CStyle.cpp:
#include "Cards_CStyle.h"
using namespace std;
//function
int* Cards::function2(int arr[]){
int a=12, b=13, c=14;
arr[0] = a;
arr[1] = b;
arr[2] = c;
return arr;
}
main_CStyle.cpp:
#include <iostream>
#include "Cards_CStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
int arr[3]; // Take care that all your functions use size <= 3
Cards cards;
int* list=cards.function2(arr);
cout<<list[1]<<endl;
return 0;
}
As recommended in the comments, you should use the containers of the STL, e.g. array for fixed length or vector for variable length. Assuming that fixed length of 3 will be fine for you, then your code would be looking like this:
Cards_STLStyle.h:
#ifndef Cards_STLStyle_H
#define Cards_STLStyle_H
#include<array>
using namespace std;
typedef array<int, 3> my_array;
class Cards {
public:
my_array function2();
};
#endif
Cards_STLStyle.cpp:
#include "Cards_STLStyle.h"
using namespace std;
//function
my_array Cards::function2(){
int a=12, b=13, c=14;
return my_array { a,b,c};
}
main_STLStyle.cpp:
#include <iostream>
#include <array>
#include "Cards_STLStyle.h"
using namespace std;
int main(int argc, const char * argv[]) {
Cards cards;
my_array list=cards.function2();
cout<<list[1]<<endl;
return 0;
}
Please find more information here:
array

string return function not working - 'identifier is underfined'

Relearning C/C++ after 3 years of JavaScript (I've gotten way too comfortable..)
I'm building a test file with input.
The problem is within cTool, where the first function is not letting me return a string. I thought this was totally valid if the library is included in the header file? What am I overlooking here.
cTool.cpp
string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <stdio.h>
#include <iostream>
using namespace std;
string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}
You need to add #include "cTool.h" to cTool.cpp, not just to cMain.cpp only. Otherwise, when compiling cTool.cpp, the compiler doesn't know what a string is since it doesn't see your #include <string> and using namespace std; statements (BTW, using namespace std; in a header file is a very bad idea).
cTool.cpp
#include "cTool.h" // <-- ADD THIS!
std::string getInfo(void) {
}
void parseInfo(void (*getInfo)()) {
}
float assessInfo(float number) {
}
...
cTool.h
#pragma once
#ifndef ASSESS_GRADE_H
#define ASSESS_GRADE_H
#include <string>
#include <iostream>
std::string getInfo(void);
void parseInfo(void(*getInputFunc)());
float assessInfo(float number);
float assessInfo(char letter);
float assessInfo(int *array);
#endif
cMain.cpp
#include "cTool.h";
int main (void) {
// function call from cTool.cpp
return 0;
}

Reference to "class" is ambigous

I would like to implement a hash table example.
So for this aim, I have created one header, one hash.cpp and main.cpp files.
in my hash.cpp , I tried to run a dummy hash function which takes key value and turns into an index value. however, it throws an error(reference to 'hash' is ambiguous) whenever I try to create an object according to that hash class.
this is my main.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int main(int argc, const char * argv[]) {
hash hash_object;
int index;
index=hash_object.hash("patrickkluivert");
cout<<"index="<<index<<endl;
return 0;
}
this is my hash.cpp:
#include "hash.h"
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdio.h>
using namespace std;
int hash(string key){
int hash=0;
int index;
index=key.length();
return index;
}
this is my hash.h
#include <stdio.h>
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
#ifndef __hashtable__hash__
#define __hashtable__hash__
class hash
{
public:
int Hash(string key);
};
#endif /* defined(__hashtable__hash__) */
Your hash class symbol is clashing with std::hash
A quick fix could be using a global namespace qualifier
int main(int argc, const char * argv[]) {
::hash hash_object;
but a better and recommended one would be to stop polluting your global namespace with
using namespace std;
and just using std::cout or std::endl when you need them.
You could also create your own namespace in case you're writing a library.
Besides, you have some capital letter typos here:
index = hash_object.hash("patrickkluivert");
^ I suppose you're referring to the Hash() function here
and here
int Hash(std::string key) {
^ this needs to be capital as well
int hash = 0;
in case you want to match your declaration and avoid cast/linking errors.
Your hash class is conflicting with std::hash. Stop using using namespace std; right now. If you want to make print statements shorter, try using std::cout; using std::endl;

C++ errors: ‘string’ does not name a type

when I compile the following files, I've got the error:
ECArgs.h:36:3: error: ‘string’ does not name a type
ECArgs.h:36: ECString value(char c);
Could somebody give me any hints for the error?
ECArgs.h
#include <list>
#include "ECString.h"
class ECArgs
{
public:
ECArgs(int argc, char *argv[]);
int nargs() { return nargs_; }
bool isset(char c);
ECString value(char c);
ECString arg(int n) { return argList[n]; }
private:
int nargs_;
int nopts_;
ECString argList[32];
list<ECString> optList;
};
ECString.h
#define ECS gnu
#if ECS == gnu
#include <cstring>
#define ECString string
using namespace std;
#else
#include <bstring.h>
#define ECString string
#endif
I ran into a similar error. It was due to the fact that I had left out using namespace std;
Alternatively, one has to use std::string for all occurrences of string.
You need to add:
#include <string>
cstring includes function to manipulate C-style string. This version works:
#include <list>
#include <string>
#if ECS == gnu
#include <cstring>
#define ECString string
using namespace std;
#else
#include <bstring.h>
#define ECString string
#endif
class ECArgs
{
public:
ECArgs(int argc, char *argv[]);
int nargs() { return nargs_; }
bool isset(char c);
ECString value(char c);
ECString arg(int n) { return argList[n]; }
private:
int nargs_;
int nopts_;
ECString argList[32];
list<ECString> optList;
};
int main()
{
}

variable was not declare in the scope class in private can not be access by member function

//This is the header file (header.h)
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
~
~
//This is the cpp file (program.cpp)
#include "header.h"
#include <cstring>
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
I'm getting program.cpp:13: error: 'w' was not declared in this scope
I'm trying to just do the strcpy from data which contain some info to w which is from the private section of the class and using the member function to access them.
I'm not sure if I forgot anything and why I can't access them.
Thanks to the last answer from Sergey Vakulenko
The sequence of the header file is very important.
It should be
#include <cstring>
#include "header.h"
not
#include "header.h"
#include <cstring>
add these headers to your cpp file:
#include <stdio.h>
#include <string.h>
#include "nameofheader.h"
Edit (more full explication ):
for me, that exemple not give any error:
1.h:
class about{
char w[10][40];
public:
void get(const char core[ ][2000], int num);
};
1.cpp:
#include <stdio.h>
#include <string.h>
#include "1.h"
//This is the cpp file (program.cpp)
void about::get(const char core[ ][2000], int num){
char data[2000];
strcpy(w[0], data);
}
int main (int argc, char** argv) {
return 0;
}
compled with g++:
g++ 1.cpp -o 1
Your program, the way you are showing it to us here, should compile without problems:
ideone.com/Bj6VU
If you want more help, you should make the all of the two files you are compiling (program.cpp and header.h) available.