Im having issues with a class constructors that i define. my .cpp's and .h compile on there own but once i try to use them in a main and construct a class it throws errors.
ive read about how not properly defining a user created constructor may cause the same error but im positive that everything is correct.
joust.cpp
#include "joust.h"
2 //create class construct for knight class
3 //set his name stam and put him on a horse
4 knight::knight (string n) : equipped_wep(5,5,"Basic Blade"){
5 name = n;
6 stamina = 100;
7 mounted = true;
8 }
9
10 void knight::show_stats() {
11 if (stamina) {
12 cout << name << " is not exhausted (stamina="<< stamina<< ") and is moun ted" << endl;
13 } else {
14 cout << name << " has passed out from exhaustion" << endl;
15 }
16 }
17 void knight::show_wep() {
18 cout << name << " is using " << equipped_wep.display_wep() << endl;
19 }
20
21 void knight::equip_weapon(weapon wep) {
22 equipped_wep = wep;
joust.h
13 #ifndef JOUST_H
14 #define JOUST_H
15
16 #include <iostream>
17 #include <fstream>
18 #include <vector>
19
20 using namespace std;
21
22 class weapon {
23 public:
24 weapon(float = 1, float = 1, string = "base");
25 void set(float, float);
26 string display_wep();
27 private:
28 float effectivness;
29 float weight;
30 string name;
31 };
32
33 class knight {
34 public:
35 knight( string = "base");
36 void show_stats();
37 void show_wep();
38 void equip_weapon(weapon wep);
39 private:
40 weapon equipped_wep;
41 string name;
42 int stamina;
43 bool mounted;
44 };
45
46
47 #endif
test.cpp
#include "joust.h"
2
3 int main() {
4 //knight jim("Rob the Third");
5 //jim.show_stats();
6 weapon c(15,12,"jard");
7
8 return 0;
9 }
makefile
test: test.o joust.o
2 g++ -std=c++11 joust.o test.o -o test
3 test.o: test.cpp joust.h
4 g++ -std=c++11 test.cpp
5 joust.o: joust.cpp joust.h
6 g++ -std=c++11 -c joust.cpp
7 clean:
8 rm -f joust.o
It should of just created a weapon object bust instead threw this error
make
g++ -std=c++11 test.cpp
Undefined symbols for architecture x86_64:
"weapon::weapon(float, float, std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >)", referenced from:
_main in test-501f47.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
make: *** [test.o] Error 1
There are two problems that you have:
First you need to build all source file together, or to create object files from each source file that you then link together.
The second error is that you don't define (implement) the weapon constructor anywhere.
The first problem is solved by adding the -c flag when building the source files. The -c flags tells GCC to create object files (the files ending in .o). You have it when building the joust.cpp source file, but seem to have forgotten it for the test.cpp source file.
The second problem should be pretty obvious how to solve: Implement the constructor function.
Related
When I am trying to run the following code
#include<bits/stdc++.h>
using namespace std;
int main()
{
int n;
cin>>n;
vector<vector<int>> v(n, vector<int>(2));
cout<<2<<"\n";
return 0;
}
using the command g++ file.cpp then it is giving the following error
0 0x1022a41a0 __assert_rtn + 140
1 0x10212ba8c mach_o::relocatable::Parser<arm64>::parse(mach_o::relocatable::ParserOptions const&) + 4536
2 0x1020fdd38 mach_o::relocatable::Parser<arm64>::parse(unsigned char const*, unsigned long long, char const*, long, ld::File::Ordinal, mach_o::relocatable::ParserOptions const&) + 148
3 0x1021664ac ld::tool::InputFiles::makeFile(Options::FileInfo const&, bool) + 1468
4 0x102169360 ___ZN2ld4tool10InputFilesC2ER7Options_block_invoke + 56
5 0x1c17b41f4 _dispatch_client_callout2 + 20
6 0x1c17c8f8c _dispatch_apply_invoke_and_wait + 224
7 0x1c17c826c _dispatch_apply_with_attr_f + 1152
8 0x1c17c847c dispatch_apply + 108
9 0x1021691f4 ld::tool::InputFiles::InputFiles(Options&) + 616
10 0x1020eb6c0 main + 552
A linker snapshot was created at:
/tmp/a.out-2022-09-19-011653.ld-snapshot
ld: Assertion failed: (_file->_atomsArrayCount == computedAtomCount && "more atoms allocated than expected"), function parse, file macho_relocatable_file.cpp, line 2061.
collect2: error: ld returned 1 exit status
Ok so, im supposed to define a variable HASH_TABLE_SIZE at link time, so I would put that in my makefile.
my Makefile is as follows:
1 CC = g++
2 CFLAGS = -c -g -std=c++11 -Wall -W -Werror -pedantic -D HASH_TABLE_SIZE=10
3 LDFLAGS = -lrt
4
5 myhash : main.o hash.o hash_function.o
6 $(CC) $(LDFLAGS) main.o hash.o hash_function.o -o myhash
7
8 main.o : main.cpp hash.h
9 $(CC) $(LDFLAGS) main.cpp
10
11 hash.o : hash.cpp hash.h
12 $(CC) $(LDFLAGS) hash.cpp
13
14 hash_function.o : hash_function.cpp hash.h
15 $(CC) $(LDFLAGS) hash_function.cpp
16
17 clean :
18 rm *.o myhash
my Makefile seems correct to me, and I am putting -D HASH_TABLE_SIZE=10.
However, when I do make, I get this error:
In file included from main.cpp:3:0:
hash.h:24:27: error: 'HASH_TABLE_SIZE' was not declared in this scope
list<string> hashTable[HASH_TABLE_SIZE];
my Hash.h file is as follows:
1 /* This assignment originated at UC Riverside. The hash table size
2 should be defined at link time. Use -D HASH_TABLE_SIZE=X */
3
4 #ifndef __HASH_H
5 #define __HASH_H
6
7 #include <string>
8 #include <list>
9
10 using namespace std;
11
12 class Hash {
13
14 public:
15 Hash();
16 void remove(string word);
17 void print();
18 void processFile(string fileName);
19 bool search(string word);
20 void output(string fileName);
21 void printStats();
22
23 private:
24 list<string> hashTable[HASH_TABLE_SIZE];
25 int collisions;
26 int longestList;
27 double avgLength;
28
29 private:
30 int hf(string ins);
31 double newAvgListLen;
32
33 // put additional variables/functions below
34 // do not change anything above!
35
36 };
37
38 #endif
Why is this happening? Any help would be greatly appreciated.
The quick solution
You are not using CFLAGS in your recipes so it is having no effect.
It should work if you change lines 8 to 15 to:
8 main.o : main.cpp hash.h
9 $(CC) $(CFLAGS) main.cpp
10
11 hash.o : hash.cpp hash.h
12 $(CC) $(CFLAGS) hash.cpp
13
14 hash_function.o : hash_function.cpp hash.h
15 $(CC) $(CFLAGS) hash_function.cpp
Some extra details
-D is a compile time flag not a link time option.
CFLAGS is conventionally used to pass compile time flags to the compiler for a C program.
Normally CXXFLAGS would be used for a C++ program such as this and CXX to specify the C++ compiler. Make doesn't really mind though but it can make it easier to follow when conventions are used.
LDFLAGS is normally used for passing link time flags like -lrt so it only makes sense to use that on line 6, not in the compiling steps.
I'm trying a very simple example with LLVM/clang and seem to fail.
I try the following:
clang++ -emit-llvm -c -x c++ -o main.bc -isystem include/ main.cc
clang++ -emit-llvm -c -x c++ -o test_class.bc -isystem include/ test_class.cc
llvm-link main.bc test_class.bc -o all.bc
lli all.bc
However, 4. fails (segfaults) with:
0 libLLVM-3.3.so 0x0000003b890f9e52 llvm::sys::PrintStackTrace(_IO_FILE*) + 34
1 libLLVM-3.3.so 0x0000003b890f9cb9
2 libpthread.so.0 0x0000003b8520efa0
3 libLLVM-3.3.so 0x0000003b89982790 llvm::MachineJumpTableInfo::getEntrySize(llvm::DataLayout const&) const + 0
4 libLLVM-3.3.so 0x0000003b894bfb23
5 libLLVM-3.3.so 0x0000003b894c8dc3
6 libLLVM-3.3.so 0x0000003b8981b27f
7 libLLVM-3.3.so 0x0000003b8969d2d6 llvm::FPPassManager::runOnFunction(llvm::Function&) + 422
8 libLLVM-3.3.so 0x0000003b8969d3f6 llvm::FunctionPassManagerImpl::run(llvm::Function&) + 102
9 libLLVM-3.3.so 0x0000003b8969d4cb llvm::FunctionPassManager::run(llvm::Function&) + 91
10 libLLVM-3.3.so 0x0000003b894b3264 llvm::JIT::jitTheFunction(llvm::Function*, llvm::MutexGuard const&) + 36
11 libLLVM-3.3.so 0x0000003b894b394f llvm::JIT::runJITOnFunctionUnlocked(llvm::Function*, llvm::MutexGuard const&) + 15
12 libLLVM-3.3.so 0x0000003b894b3b7e llvm::JIT::getPointerToFunction(llvm::Function*) + 254
13 libLLVM-3.3.so 0x0000003b894c6649
14 libLLVM-3.3.so 0x0000003b894c909c
15 libLLVM-3.3.so 0x0000003b8981b27f
16 libLLVM-3.3.so 0x0000003b8969d2d6 llvm::FPPassManager::runOnFunction(llvm::Function&) + 422
17 libLLVM-3.3.so 0x0000003b8969d3f6 llvm::FunctionPassManagerImpl::run(llvm::Function&) + 102
18 libLLVM-3.3.so 0x0000003b8969d4cb llvm::FunctionPassManager::run(llvm::Function&) + 91
19 libLLVM-3.3.so 0x0000003b894b3264 llvm::JIT::jitTheFunction(llvm::Function*, llvm::MutexGuard const&) + 36
20 libLLVM-3.3.so 0x0000003b894b394f llvm::JIT::runJITOnFunctionUnlocked(llvm::Function*, llvm::MutexGuard const&) + 15
21 libLLVM-3.3.so 0x0000003b894b3b7e llvm::JIT::getPointerToFunction(llvm::Function*) + 254
22 lli 0x00000000004073cf main + 2527
23 libc.so.6 0x0000003b84621b75 __libc_start_main + 245
24 lli 0x000000000040a271
Stack dump:
0. Program arguments: lli all.bc
1. Running pass 'X86 Machine Code Emitter' on function '#main'
2. Running pass 'X86 Machine Code Emitter' on function '#_ZN10test_classC2ESs'
[1] 15327 segmentation fault (core dumped) lli all.bc
Do I have a fundamental missunderstanding on how this is supposed to work? My ultimate goal is to do the compilation
part using the libclang API, but for now understanding what I do wrong here would be awesome! Thanks!
I attached the source code for the example below:
include/test_class.h:
#ifndef __TEST_CLASS_H__
#define __TEST_CLASS_H__
#include <string>
#include <iostream>
struct test_class
{
test_class(std::string foo);
~test_class();
std::string foo;
};
#endif
test_class.cc:
#include <test_class.h>
test_class::test_class(std::string foo) : foo(foo)
{
std::cout << foo << std::endl;
}
test_class::~test_class(void)
{
}
main.cc:
#include <cstdlib>
#include <string>
#include <iostream>
#include <test_class.h>
int main(int argc, char** argv)
{
test_class test("foo");
return EXIT_SUCCESS;
}
In test_class you refer to std::string. I guess, calls to related functions are marked external if you have a look into the IR (eg: llvm-dis < all.bc).
If you compile to "normal" executable code, these calls are resolved by the linker.
Since you link manually with llvm-link you will have to provide IR-code for the C++ libraries, otherwise execution might fail.
I wrote the following code for a program that performs CRC encoding. I modeled it on a C program that we were taught in class. It gives some compilation errors that I can't correct. I have mentioned them after the code.
I wrote the following code for a program that performs CRC encoding. I modeled it on a C program that we were taught in class. It gives some compilation errors that I can't correct. I have mentioned them after the code.
1 #include<iostream>
2 #include<string.h>
3
4 using namespace std;
5
6 class crc
7 {
8 char message[128],polynomial[18],checksum[256];
9 public:
10 void xor()
11 {
12 for(int i=1;i<strlen(polynomial);i++)
13 checksum[i]=((checksum[i]==polynomial[i])?'0':'1');
14 }
15 crc() //constructor
16 {
17 cout<<"Enter the message:"<<endl;
18 cin>>message;
19 cout<<"Enter the polynomial";
20 cin>polynomial;
21 }
22 void compute()
23 {
24 int e,i;
25 for(e=0;e<strlen(polynomial);e++)
26 checksum[e]=message[e];
27 do
28 {
29 if(checksum[0]=='0')
30 xor();
31 for(i=0;i<(strlen(polynomial)-1);i++)
32 checksum[i]=checksum[i+1];
33 checksum[i]=message[e++];
34 }while(e<=strlen(message)+strlen(checksum)-1);
35 }
36 void gen_proc() //general processing
37 {
38 int mesg_len=strlen(message);
39 for(int i=mesg_len;i<mesg_len+strlen(polynomial)-1;i++)
40 message[i]='0';
41 message[i]='\0'; //necessary?
42 cout<<"After appending zeroes message is:"<<message;
43 compute();
44 cout<<"Checksum is:"<<checksum;
45 for(int i=mesg_len;i<mesg_len+strlen(polynomial);i++)
46 message[i]=checksum[i-mesg_len];
47 cout<<"Final codeword is:"<<message;
48 }
49 };
50 int main()
51 {
52 crc c1;
53 c1.gen_proc();
54 return 0;
55 }
The compilation errors are:
crc.cpp:10: error: expected unqualified-id before ‘^’ token
crc.cpp: In member function ‘void crc::compute()’:
crc.cpp:30: error: expected primary-expression before ‘^’ token
crc.cpp:30: error: expected primary-expression before ‘)’ token
crc.cpp: In member function ‘void crc::gen_proc()’:
crc.cpp:41: warning: name lookup of ‘i’ changed for ISO ‘for’ scoping
crc.cpp:39: warning: using obsolete binding at ‘i’
I have been checking online for these errors and the only thing I have been seeing is errors caused by incorrect array handling. I have double checked my code but I don't seem to be performing any incorrect array access.
xor is a reserved keyword in C++. You should rename the function to something else.
The compiler isn't actually "seeing" an identifier, but a keyword. If, in your code snippet, you'd replace xor with ^ the obvious syntactic error becomes clear.
I am using Ubuntu 10.10, Codeblocks IDE, and gcc compiler. I noticed the program I am writing was creating some odd output. Eventually I narrowed the issue down to a for-loop in the program. I was surprised to discover that the following basic for-loop didn't perform as expected.
#include <iostream>
using namespace std;
int main()
{
for(unsigned int i = 0; i < 21; i++)
{
cout << i << endl;
}
return 0;
}
When I compile and run it, the output is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Although one would expect the output should include zero. Very surprisingly, when I change the for loop to
#include <iostream>
using namespace std;
int main()
{
for(unsigned int i = 0; i < 20; i++)
{
cout << i << endl;
}
return 0;
}
I get the expected output of:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
I can't for the life of me figure out why 21 (and all numbers greater than 21) give me this false output, while 20 (and lower numbers) don't. If anyone has run into anything like this before, I'd sure appreciate hearing how he/she worked around it.
maybe the screen just scroll?
try to redirect the output to a text file
This seemed so weird that i run your first program and got what i would expect :
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
However, i notice that you use gcc as your compiler. This one is aimed towards c programming. Better use g++ as i did for this. It works fine here. (i'm actually surprised gcc compiles that :/)